home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / prog / gnu-c / src / gcc-2.7.0-amiga / cccp.c < prev    next >
C/C++ Source or Header  |  1995-08-24  |  287KB  |  10,535 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2.    Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
  3.    Written by Paul Rubin, June 1986
  4.    Adapted to ANSI C, Richard Stallman, Jan 1987
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.
  20.  
  21.  In other words, you are welcome to use, share and improve this program.
  22.  You are forbidden to forbid anyone else to use, share and improve
  23.  what you give them.   Help stamp out software-hoarding!  */
  24.  
  25. typedef unsigned char U_CHAR;
  26.  
  27. #ifdef EMACS
  28. #define NO_SHORTNAMES
  29. #include "../src/config.h"
  30. #ifdef open
  31. #undef open
  32. #undef read
  33. #undef write
  34. #endif /* open */
  35. #endif /* EMACS */
  36.  
  37. /* The macro EMACS is defined when cpp is distributed as part of Emacs,
  38.    for the sake of machines with limited C compilers.  */
  39. #ifndef EMACS
  40. #include "config.h"
  41. #endif /* not EMACS */
  42.  
  43. #ifdef INHIBIT_ALLOCA_USE
  44. /* Since cpp uses alloca to store all its read files, this is quite deadly
  45.    on a system with non-automatic stackgrowth like amigados, so we better
  46.    turn it off now.  Normally alloca is #defined to __builtin_alloca, so
  47.    undefining it causes an external alloca to be used.
  48.  
  49.    Note that it's not wise to generally inhibit __builtin_alloca, since
  50.    using the generic emulator results in a serious (!) speed penalty, and
  51.    it's bad enough that we have to live with it in cccp, don't make cc1
  52.    unbearably slow as well... */
  53.  
  54. #undef alloca
  55. #endif
  56.  
  57. #ifdef __amigados__
  58. static int amigados_abs_filename ();
  59. #endif
  60.  
  61. #ifndef STANDARD_INCLUDE_DIR
  62. #define STANDARD_INCLUDE_DIR "/usr/include"
  63. #endif
  64.  
  65. #ifndef LOCAL_INCLUDE_DIR
  66. #define LOCAL_INCLUDE_DIR "/usr/local/include"
  67. #endif
  68.  
  69. #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE.  */
  70. #ifdef __STDC__
  71. #define PTR_INT_TYPE ptrdiff_t
  72. #else
  73. #define PTR_INT_TYPE long
  74. #endif
  75. #endif /* 0 */
  76.  
  77. #include "pcp.h"
  78.  
  79. /* By default, colon separates directories in a path.  */
  80. #ifndef PATH_SEPARATOR
  81. #define PATH_SEPARATOR ':'
  82. #endif
  83.  
  84. #include <sys/types.h>
  85. #include <sys/stat.h>
  86. #include <ctype.h>
  87. #include <stdio.h>
  88. #include <signal.h>
  89.  
  90. /* The following symbols should be autoconfigured:
  91.     HAVE_FCNTL_H
  92.     HAVE_STDLIB_H
  93.     HAVE_SYS_TIME_H
  94.     HAVE_UNISTD_H
  95.     STDC_HEADERS
  96.     TIME_WITH_SYS_TIME
  97.    In the mean time, we'll get by with approximations based
  98.    on existing GCC configuration symbols.  */
  99.  
  100. #ifdef POSIX
  101. # ifndef HAVE_STDLIB_H
  102. # define HAVE_STDLIB_H 1
  103. # endif
  104. # ifndef HAVE_UNISTD_H
  105. # define HAVE_UNISTD_H 1
  106. # endif
  107. # ifndef STDC_HEADERS
  108. # define STDC_HEADERS 1
  109. # endif
  110. #endif /* defined (POSIX) */
  111.  
  112. #if defined (POSIX) || (defined (USG) && !defined (VMS))
  113. # ifndef HAVE_FCNTL_H
  114. # define HAVE_FCNTL_H 1
  115. # endif
  116. #endif
  117.  
  118. #ifndef RLIMIT_STACK
  119. # include <time.h>
  120. #else
  121. # if TIME_WITH_SYS_TIME
  122. #  include <sys/time.h>
  123. #  include <time.h>
  124. # else
  125. #  if HAVE_SYS_TIME_H
  126. #   include <sys/time.h>
  127. #  else
  128. #   include <time.h>
  129. #  endif
  130. # endif
  131. # include <sys/resource.h>
  132. #endif
  133.  
  134. #if HAVE_FCNTL_H
  135. # include <fcntl.h>
  136. #endif
  137.  
  138. /* This defines "errno" properly for VMS, and gives us EACCES. */
  139. #include <errno.h>
  140.  
  141. #if HAVE_STDLIB_H
  142. # include <stdlib.h>
  143. #else
  144. char *getenv ();
  145. #endif
  146.  
  147. #if STDC_HEADERS
  148. # include <string.h>
  149. # ifndef bcmp
  150. # define bcmp(a, b, n) memcmp (a, b, n)
  151. # endif
  152. # ifndef bcopy
  153. # define bcopy(s, d, n) memcpy (d, s, n)
  154. # endif
  155. # ifndef bzero
  156. # define bzero(d, n) memset (d, 0, n)
  157. # endif
  158. #else /* !STDC_HEADERS */
  159. char *index ();
  160. char *rindex ();
  161.  
  162. # if !defined (BSTRING) && (defined (USG) || defined (VMS))
  163.  
  164. #  ifndef bcmp
  165. #  define bcmp my_bcmp
  166. static int
  167. my_bcmp (a, b, n)
  168.      register char *a;
  169.      register char *b;
  170.      register unsigned n;
  171. {
  172.    while (n-- > 0)
  173.      if (*a++ != *b++)
  174.        return 1;
  175.  
  176.    return 0;
  177. }
  178. #  endif /* !defined (bcmp) */
  179.  
  180. #  ifndef bcopy
  181. #  define bcopy my_bcopy
  182. static void
  183. my_bcopy (s, d, n)
  184.      register char *s;
  185.      register char *d;
  186.      register unsigned n;
  187. {
  188.   while (n-- > 0)
  189.     *d++ = *s++;
  190. }
  191. #  endif /* !defined (bcopy) */
  192.  
  193. #  ifndef bzero
  194. #  define bzero my_bzero
  195. static void
  196. my_bzero (b, length)
  197.      register char *b;
  198.      register unsigned length;
  199. {
  200.   while (length-- > 0)
  201.     *b++ = 0;
  202. }
  203. #  endif /* !defined (bzero) */
  204.  
  205. # endif /* !defined (BSTRING) && (defined (USG) || defined (VMS)) */
  206. #endif /* ! STDC_HEADERS */
  207.  
  208. #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6)
  209. # define __attribute__(x)
  210. #endif
  211.  
  212. #ifndef PROTO
  213. # if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  214. #  define PROTO(ARGS) ARGS
  215. # else
  216. #  define PROTO(ARGS) ()
  217. # endif
  218. #endif
  219.  
  220. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  221. # include <stdarg.h>
  222. # define VA_START(va_list, var) va_start (va_list, var)
  223. # define PRINTF_ALIST(msg) char *msg, ...
  224. # define PRINTF_DCL(msg)
  225. # define PRINTF_PROTO(ARGS, m, n) PROTO (ARGS) __attribute__ ((format (printf, m, n)))
  226. #else
  227. # include <varargs.h>
  228. # define VA_START(va_list, var) va_start (va_list)
  229. # define PRINTF_ALIST(msg) msg, va_alist
  230. # define PRINTF_DCL(msg) char *msg; va_dcl
  231. # define PRINTF_PROTO(ARGS, m, n) () __attribute__ ((format (printf, m, n)))
  232. # define vfprintf(file, msg, args) \
  233.     { \
  234.       char *a0 = va_arg(args, char *); \
  235.       char *a1 = va_arg(args, char *); \
  236.       char *a2 = va_arg(args, char *); \
  237.       char *a3 = va_arg(args, char *); \
  238.       fprintf (file, msg, a0, a1, a2, a3); \
  239.     }
  240. #endif
  241.  
  242. #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
  243. #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
  244. #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
  245.  
  246. #if HAVE_UNISTD_H
  247. # include <unistd.h>
  248. #endif
  249.  
  250. /* VMS-specific definitions */
  251. #ifdef VMS
  252. #include <descrip.h>
  253. #define O_RDONLY    0    /* Open arg for Read/Only  */
  254. #define O_WRONLY    1    /* Open arg for Write/Only */
  255. #define read(fd,buf,size)    VMS_read (fd,buf,size)
  256. #define write(fd,buf,size)    VMS_write (fd,buf,size)
  257. #define open(fname,mode,prot)    VMS_open (fname,mode,prot)
  258. #define fopen(fname,mode)    VMS_fopen (fname,mode)
  259. #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
  260. #define strncat(dst,src,cnt) VMS_strncat (dst,src,cnt)
  261. #define fstat(fd,stbuf)        VMS_fstat (fd,stbuf)
  262. static int VMS_fstat (), VMS_stat ();
  263. static char * VMS_strncat ();
  264. static int VMS_read ();
  265. static int VMS_write ();
  266. static int VMS_open ();
  267. static FILE * VMS_fopen ();
  268. static FILE * VMS_freopen ();
  269. static void hack_vms_include_specification ();
  270. typedef struct { unsigned :16, :16, :16; } vms_ino_t;
  271. #define ino_t vms_ino_t
  272. #define INCLUDE_LEN_FUDGE 10    /* leave room for VMS syntax conversion */
  273. #ifdef __GNUC__
  274. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  275. #endif /* __GNUC__ */
  276. #endif /* VMS */
  277.  
  278. #ifndef O_RDONLY
  279. #define O_RDONLY 0
  280. #endif
  281.  
  282. #undef MIN
  283. #undef MAX
  284. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  285. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  286.  
  287. /* Find the largest host integer type and set its size and type.  */
  288.  
  289. #ifndef HOST_BITS_PER_WIDE_INT
  290.  
  291. #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
  292. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
  293. #define HOST_WIDE_INT long
  294. #else
  295. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
  296. #define HOST_WIDE_INT int
  297. #endif
  298.  
  299. #endif
  300.  
  301. #ifndef S_ISREG
  302. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  303. #endif
  304.  
  305. #ifndef S_ISDIR
  306. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  307. #endif
  308.  
  309. /* Define a generic NULL if one hasn't already been defined.  */
  310.  
  311. #ifndef NULL
  312. #define NULL 0
  313. #endif
  314.  
  315. #ifndef GENERIC_PTR
  316. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  317. #define GENERIC_PTR void *
  318. #else
  319. #define GENERIC_PTR char *
  320. #endif
  321. #endif
  322.  
  323. #ifndef NULL_PTR
  324. #define NULL_PTR ((GENERIC_PTR)0)
  325. #endif
  326.  
  327. #ifndef INCLUDE_LEN_FUDGE
  328. #define INCLUDE_LEN_FUDGE 0
  329. #endif
  330.  
  331. /* External declarations.  */
  332.  
  333. extern char *version_string;
  334. #ifndef VMS
  335. #ifndef HAVE_STRERROR
  336. extern int sys_nerr;
  337. #if defined(bsd4_4)
  338. extern const char *const sys_errlist[];
  339. #else
  340. extern char *sys_errlist[];
  341. #endif
  342. #else    /* HAVE_STRERROR */
  343. char *strerror ();
  344. #endif
  345. #else    /* VMS */
  346. char *strerror (int,...);
  347. #endif
  348. int parse_escape PROTO((char **));
  349. HOST_WIDE_INT parse_c_expression PROTO((char *));
  350.  
  351. #ifndef errno
  352. extern int errno;
  353. #endif
  354.  
  355. /* Name under which this program was invoked.  */
  356.  
  357. static char *progname;
  358.  
  359. /* Nonzero means use extra default include directories for C++.  */
  360.  
  361. static int cplusplus;
  362.  
  363. /* Nonzero means handle cplusplus style comments */
  364.  
  365. static int cplusplus_comments;
  366.  
  367. /* Nonzero means handle #import, for objective C.  */
  368.  
  369. static int objc;
  370.  
  371. /* Nonzero means this is an assembly file, and allow
  372.    unknown directives, which could be comments.  */
  373.  
  374. static int lang_asm;
  375.  
  376. /* Current maximum length of directory names in the search path
  377.    for include files.  (Altered as we get more of them.)  */
  378.  
  379. static int max_include_len;
  380.  
  381. #ifdef __amigados__
  382. /* Phil.B: 03-Oct-94 Flag indicating process priority */
  383. static int amiga_priority = -1;
  384. static int amiga_old_priority;
  385. #include <proto/exec.h>
  386. struct Task *amiga_task;
  387. #endif /* __amigados__ */
  388.  
  389. /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
  390.  
  391. static int for_lint = 0;
  392.  
  393. /* Nonzero means copy comments into the output file.  */
  394.  
  395. static int put_out_comments = 0;
  396.  
  397. /* Nonzero means don't process the ANSI trigraph sequences.  */
  398.  
  399. static int no_trigraphs = 0;
  400.  
  401. /* Nonzero means print the names of included files rather than
  402.    the preprocessed output.  1 means just the #include "...",
  403.    2 means #include <...> as well.  */
  404.  
  405. static int print_deps = 0;
  406.  
  407. /* Nonzero if missing .h files in -M output are assumed to be generated
  408.    files and not errors.  */
  409.  
  410. static int print_deps_missing_files = 0;
  411.  
  412. /* Nonzero means print names of header files (-H).  */
  413.  
  414. static int print_include_names = 0;
  415.  
  416. /* Nonzero means don't output line number information.  */
  417.  
  418. static int no_line_directives;
  419.  
  420. /* Nonzero means output the text in failing conditionals,
  421.    inside #failed ... #endfailed.  */
  422.  
  423. static int output_conditionals;
  424.  
  425. /* dump_only means inhibit output of the preprocessed text
  426.              and instead output the definitions of all user-defined
  427.              macros in a form suitable for use as input to cccp.
  428.    dump_names means pass #define and the macro name through to output.
  429.    dump_definitions means pass the whole definition (plus #define) through
  430. */
  431.  
  432. static enum {dump_none, dump_only, dump_names, dump_definitions}
  433.      dump_macros = dump_none;
  434.  
  435. /* Nonzero means pass all #define and #undef directives which we actually
  436.    process through to the output stream.  This feature is used primarily
  437.    to allow cc1 to record the #defines and #undefs for the sake of
  438.    debuggers which understand about preprocessor macros, but it may
  439.    also be useful with -E to figure out how symbols are defined, and
  440.    where they are defined.  */
  441. static int debug_output = 0;
  442.  
  443. /* Nonzero indicates special processing used by the pcp program.  The
  444.    special effects of this mode are: 
  445.      
  446.      Inhibit all macro expansion, except those inside #if directives.
  447.  
  448.      Process #define directives normally, and output their contents 
  449.      to the output file.
  450.  
  451.      Output preconditions to pcp_outfile indicating all the relevant
  452.      preconditions for use of this file in a later cpp run.
  453. */
  454. static FILE *pcp_outfile;
  455.  
  456. /* Nonzero means we are inside an IF during a -pcp run.  In this mode
  457.    macro expansion is done, and preconditions are output for all macro
  458.    uses requiring them. */
  459. static int pcp_inside_if;
  460.  
  461. /* Nonzero means never to include precompiled files.
  462.    This is 1 since there's no way now to make precompiled files,
  463.    so it's not worth testing for them.  */
  464. static int no_precomp = 1;
  465.  
  466. /* Nonzero means give all the error messages the ANSI standard requires.  */
  467.  
  468. int pedantic;
  469.  
  470. /* Nonzero means try to make failure to fit ANSI C an error.  */
  471.  
  472. static int pedantic_errors;
  473.  
  474. /* Nonzero means don't print warning messages.  -w.  */
  475.  
  476. static int inhibit_warnings = 0;
  477.  
  478. /* Nonzero means warn if slash-star appears in a comment.  */
  479.  
  480. static int warn_comments;
  481.  
  482. /* Nonzero means warn if a macro argument is (or would be)
  483.    stringified with -traditional.  */
  484.  
  485. static int warn_stringify;
  486.  
  487. /* Nonzero means warn if there are any trigraphs.  */
  488.  
  489. static int warn_trigraphs;
  490.  
  491. /* Nonzero means warn if #import is used.  */
  492.  
  493. static int warn_import = 1;
  494.  
  495. /* Nonzero means turn warnings into errors.  */
  496.  
  497. static int warnings_are_errors;
  498.  
  499. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  500.  
  501. int traditional;
  502.  
  503. /* Nonzero causes output not to be done,
  504.    but directives such as #define that have side effects
  505.    are still obeyed.  */
  506.  
  507. static int no_output;
  508.  
  509. /* Nonzero means this file was included with a -imacros or -include
  510.    command line and should not be recorded as an include file.  */
  511.  
  512. static int no_record_file;
  513.  
  514. /* Nonzero means that we have finished processing the command line options.
  515.    This flag is used to decide whether or not to issue certain errors
  516.    and/or warnings.  */
  517.  
  518. static int done_initializing = 0;
  519.  
  520. /* Line where a newline was first seen in a string constant.  */
  521.  
  522. static int multiline_string_line = 0;
  523.  
  524. /* I/O buffer structure.
  525.    The `fname' field is nonzero for source files and #include files
  526.    and for the dummy text used for -D and -U.
  527.    It is zero for rescanning results of macro expansion
  528.    and for expanding macro arguments.  */
  529. #define INPUT_STACK_MAX 400
  530. static struct file_buf {
  531.   char *fname;
  532.   /* Filename specified with #line directive.  */
  533.   char *nominal_fname;
  534.   /* Record where in the search path this file was found.
  535.      For #include_next.  */
  536.   struct file_name_list *dir;
  537.   int lineno;
  538.   int length;
  539.   U_CHAR *buf;
  540.   U_CHAR *bufp;
  541.   /* Macro that this level is the expansion of.
  542.      Included so that we can reenable the macro
  543.      at the end of this level.  */
  544.   struct hashnode *macro;
  545.   /* Value of if_stack at start of this file.
  546.      Used to prohibit unmatched #endif (etc) in an include file.  */
  547.   struct if_stack *if_stack;
  548.   /* Object to be freed at end of input at this level.  */
  549.   U_CHAR *free_ptr;
  550.   /* True if this is a header file included using <FILENAME>.  */
  551.   char system_header_p;
  552. } instack[INPUT_STACK_MAX];
  553.  
  554. static int last_error_tick;       /* Incremented each time we print it.  */
  555. static int input_file_stack_tick;  /* Incremented when the status changes.  */
  556.  
  557. /* Current nesting level of input sources.
  558.    `instack[indepth]' is the level currently being read.  */
  559. static int indepth = -1;
  560. #define CHECK_DEPTH(code) \
  561.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  562.     {                                    \
  563.       error_with_line (line_for_error (instack[indepth].lineno),    \
  564.                "macro or `#include' recursion too deep");    \
  565.       code;                                \
  566.     }
  567.  
  568. /* Current depth in #include directives that use <...>.  */
  569. static int system_include_depth = 0;
  570.  
  571. typedef struct file_buf FILE_BUF;
  572.  
  573. /* The output buffer.  Its LENGTH field is the amount of room allocated
  574.    for the buffer, not the number of chars actually present.  To get
  575.    that, subtract outbuf.buf from outbuf.bufp. */
  576.  
  577. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  578. static FILE_BUF outbuf;
  579.  
  580. /* Grow output buffer OBUF points at
  581.    so it can hold at least NEEDED more chars.  */
  582.  
  583. #define check_expand(OBUF, NEEDED)  \
  584.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  585.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  586.  
  587. struct file_name_list
  588.   {
  589.     struct file_name_list *next;
  590.     char *fname;
  591.     /* If the following is nonzero, it is a macro name.
  592.        Don't include the file again if that macro is defined.  */
  593.     U_CHAR *control_macro;
  594.     /* If the following is nonzero, it is a C-language system include
  595.        directory.  */
  596.     int c_system_include_path;
  597.     /* Mapping of file names for this directory.  */
  598.     struct file_name_map *name_map;
  599.     /* Non-zero if name_map is valid.  */
  600.     int got_name_map;
  601.   };
  602.  
  603. /* #include "file" looks in source file dir, then stack. */
  604. /* #include <file> just looks in the stack. */
  605. /* -I directories are added to the end, then the defaults are added. */
  606. /* The */
  607. static struct default_include {
  608.   char *fname;            /* The name of the directory.  */
  609.   int cplusplus;        /* Only look here if we're compiling C++.  */
  610.   int cxx_aware;        /* Includes in this directory don't need to
  611.                    be wrapped in extern "C" when compiling
  612.                    C++.  */
  613. } include_defaults_array[]
  614. #ifdef INCLUDE_DEFAULTS
  615.   = INCLUDE_DEFAULTS;
  616. #else
  617.   = {
  618.     /* Pick up GNU C++ specific include files.  */
  619.     { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
  620. #ifdef CROSS_COMPILE
  621.     /* This is the dir for fixincludes.  Put it just before
  622.        the files that we fix.  */
  623.     { GCC_INCLUDE_DIR, 0, 0 },
  624.     /* For cross-compilation, this dir name is generated
  625.        automatically in Makefile.in.  */
  626.     { CROSS_INCLUDE_DIR, 0, 0 },
  627.     /* This is another place that the target system's headers might be.  */
  628.     { TOOL_INCLUDE_DIR, 0, 0 },
  629. #else /* not CROSS_COMPILE */
  630.     /* This should be /usr/local/include and should come before
  631.        the fixincludes-fixed header files.  */
  632.     { LOCAL_INCLUDE_DIR, 0, 1 },
  633.     /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
  634.        Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
  635.     { TOOL_INCLUDE_DIR, 0, 0 },
  636.     /* This is the dir for fixincludes.  Put it just before
  637.        the files that we fix.  */
  638.     { GCC_INCLUDE_DIR, 0, 0 },
  639.     /* Some systems have an extra dir of include files.  */
  640. #ifdef SYSTEM_INCLUDE_DIR
  641.     { SYSTEM_INCLUDE_DIR, 0, 0 },
  642. #endif
  643.     { STANDARD_INCLUDE_DIR, 0, 0 },
  644. #endif /* not CROSS_COMPILE */
  645.     { 0, 0, 0 }
  646.     };
  647. #endif /* no INCLUDE_DEFAULTS */
  648.  
  649. /* The code looks at the defaults through this pointer, rather than through
  650.    the constant structure above.  This pointer gets changed if an environment
  651.    variable specifies other defaults.  */
  652. static struct default_include *include_defaults = include_defaults_array;
  653.  
  654. static struct file_name_list *include = 0;    /* First dir to search */
  655.     /* First dir to search for <file> */
  656. /* This is the first element to use for #include <...>.
  657.    If it is 0, use the entire chain for such includes.  */
  658. static struct file_name_list *first_bracket_include = 0;
  659. /* This is the first element in the chain that corresponds to
  660.    a directory of system header files.  */
  661. static struct file_name_list *first_system_include = 0;
  662. static struct file_name_list *last_include = 0;    /* Last in chain */
  663.  
  664. /* Chain of include directories to put at the end of the other chain.  */
  665. static struct file_name_list *after_include = 0;
  666. static struct file_name_list *last_after_include = 0;    /* Last in chain */
  667.  
  668. /* Chain to put at the start of the system include files.  */
  669. static struct file_name_list *before_system = 0;
  670. static struct file_name_list *last_before_system = 0;    /* Last in chain */
  671.  
  672. /* List of included files that contained #pragma once.  */
  673. static struct file_name_list *dont_repeat_files = 0;
  674.  
  675. /* List of other included files.
  676.    If ->control_macro if nonzero, the file had a #ifndef
  677.    around the entire contents, and ->control_macro gives the macro name.  */
  678. static struct file_name_list *all_include_files = 0;
  679.  
  680. /* Directory prefix that should replace `/usr' in the standard
  681.    include file directories.  */
  682. static char *include_prefix;
  683.  
  684. /* Global list of strings read in from precompiled files.  This list
  685.    is kept in the order the strings are read in, with new strings being
  686.    added at the end through stringlist_tailp.  We use this list to output
  687.    the strings at the end of the run. 
  688. */
  689. static STRINGDEF *stringlist;
  690. static STRINGDEF **stringlist_tailp = &stringlist;
  691.  
  692.  
  693. /* Structure returned by create_definition */
  694. typedef struct macrodef MACRODEF;
  695. struct macrodef
  696. {
  697.   struct definition *defn;
  698.   U_CHAR *symnam;
  699.   int symlen;
  700. };
  701.  
  702. enum sharp_token_type {
  703.   NO_SHARP_TOKEN = 0,        /* token not present */
  704.  
  705.   SHARP_TOKEN = '#',        /* token spelled with # only */
  706.   WHITE_SHARP_TOKEN,        /* token spelled with # and white space */
  707.  
  708.   PERCENT_COLON_TOKEN = '%',    /* token spelled with %: only */
  709.   WHITE_PERCENT_COLON_TOKEN    /* token spelled with %: and white space */
  710. };
  711.  
  712. /* Structure allocated for every #define.  For a simple replacement
  713.    such as
  714.        #define foo bar ,
  715.    nargs = -1, the `pattern' list is null, and the expansion is just
  716.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  717.    e.g.,
  718.        #define getchar() getc (stdin) .
  719.    When there are args, the expansion is the replacement text with the
  720.    args squashed out, and the reflist is a list describing how to
  721.    build the output from the input: e.g., "3 chars, then the 1st arg,
  722.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  723.    The chars here come from the expansion.  Whatever is left of the
  724.    expansion after the last arg-occurrence is copied after that arg.
  725.    Note that the reflist can be arbitrarily long---
  726.    its length depends on the number of times the arguments appear in
  727.    the replacement text, not how many args there are.  Example:
  728.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  729.    pattern list
  730.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  731.    where (x, y) means (nchars, argno). */
  732.  
  733. typedef struct definition DEFINITION;
  734. struct definition {
  735.   int nargs;
  736.   int length;            /* length of expansion string */
  737.   int predefined;        /* True if the macro was builtin or */
  738.                 /* came from the command line */
  739.   U_CHAR *expansion;
  740.   int line;            /* Line number of definition */
  741.   char *file;            /* File of definition */
  742.   char rest_args;        /* Nonzero if last arg. absorbs the rest */
  743.   struct reflist {
  744.     struct reflist *next;
  745.  
  746.     enum sharp_token_type stringify;    /* set if a # operator before arg */
  747.     enum sharp_token_type raw_before;    /* set if a ## operator before arg */
  748.     enum sharp_token_type raw_after;    /* set if a ## operator after arg */
  749.  
  750.     char rest_args;        /* Nonzero if this arg. absorbs the rest */
  751.     int nchars;            /* Number of literal chars to copy before
  752.                    this arg occurrence.  */
  753.     int argno;            /* Number of arg to substitute (origin-0) */
  754.   } *pattern;
  755.   union {
  756.     /* Names of macro args, concatenated in reverse order
  757.        with comma-space between them.
  758.        The only use of this is that we warn on redefinition
  759.        if this differs between the old and new definitions.  */
  760.     U_CHAR *argnames;
  761.   } args;
  762. };
  763.  
  764. /* different kinds of things that can appear in the value field
  765.    of a hash node.  Actually, this may be useless now. */
  766. union hashval {
  767.   char *cpval;
  768.   DEFINITION *defn;
  769.   KEYDEF *keydef;
  770. };
  771.  
  772. /*
  773.  * special extension string that can be added to the last macro argument to 
  774.  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
  775.  *         #define wow(a, b...)        process (b, a, b)
  776.  *        { wow (1, 2, 3); }    ->    { process (2, 3, 1, 2, 3); }
  777.  *        { wow (one, two); }    ->    { process (two, one, two); }
  778.  * if this "rest_arg" is used with the concat token '##' and if it is not
  779.  * supplied then the token attached to with ## will not be outputted.  Ex:
  780.  *         #define wow (a, b...)        process (b ## , a, ## b)
  781.  *        { wow (1, 2); }        ->    { process (2, 1, 2); }
  782.  *        { wow (one); }        ->    { process (one); {
  783.  */
  784. static char rest_extension[] = "...";
  785. #define REST_EXTENSION_LENGTH    (sizeof (rest_extension) - 1)
  786.  
  787. /* The structure of a node in the hash table.  The hash table
  788.    has entries for all tokens defined by #define directives (type T_MACRO),
  789.    plus some special tokens like __LINE__ (these each have their own
  790.    type, and the appropriate code is run when that type of node is seen.
  791.    It does not contain control words like "#define", which are recognized
  792.    by a separate piece of code. */
  793.  
  794. /* different flavors of hash nodes --- also used in keyword table */
  795. enum node_type {
  796.  T_DEFINE = 1,    /* the `#define' keyword */
  797.  T_INCLUDE,    /* the `#include' keyword */
  798.  T_INCLUDE_NEXT, /* the `#include_next' keyword */
  799.  T_IMPORT,      /* the `#import' keyword */
  800.  T_IFDEF,    /* the `#ifdef' keyword */
  801.  T_IFNDEF,    /* the `#ifndef' keyword */
  802.  T_IF,        /* the `#if' keyword */
  803.  T_ELSE,    /* `#else' */
  804.  T_PRAGMA,    /* `#pragma' */
  805.  T_ELIF,    /* `#elif' */
  806.  T_UNDEF,    /* `#undef' */
  807.  T_LINE,    /* `#line' */
  808.  T_ERROR,    /* `#error' */
  809.  T_WARNING,    /* `#warning' */
  810.  T_ENDIF,    /* `#endif' */
  811.  T_SCCS,    /* `#sccs', used on system V.  */
  812.  T_IDENT,    /* `#ident', used on system V.  */
  813.  T_ASSERT,    /* `#assert', taken from system V.  */
  814.  T_UNASSERT,    /* `#unassert', taken from system V.  */
  815.  T_SPECLINE,    /* special symbol `__LINE__' */
  816.  T_DATE,    /* `__DATE__' */
  817.  T_FILE,    /* `__FILE__' */
  818.  T_BASE_FILE,    /* `__BASE_FILE__' */
  819.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  820.  T_VERSION,    /* `__VERSION__' */
  821.  T_SIZE_TYPE,   /* `__SIZE_TYPE__' */
  822.  T_PTRDIFF_TYPE,   /* `__PTRDIFF_TYPE__' */
  823.  T_WCHAR_TYPE,   /* `__WCHAR_TYPE__' */
  824.  T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
  825.  T_REGISTER_PREFIX_TYPE,   /* `__REGISTER_PREFIX__' */
  826.  T_IMMEDIATE_PREFIX_TYPE,  /* `__IMMEDIATE_PREFIX__' */
  827.  T_TIME,    /* `__TIME__' */
  828.  T_CONST,    /* Constant value, used by `__STDC__' */
  829.  T_MACRO,    /* macro defined by `#define' */
  830.  T_DISABLED,    /* macro temporarily turned off for rescan */
  831.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  832.  T_PCSTRING,    /* precompiled string (hashval is KEYDEF *) */
  833.  T_UNUSED    /* Used for something not defined.  */
  834.  };
  835.  
  836. struct hashnode {
  837.   struct hashnode *next;    /* double links for easy deletion */
  838.   struct hashnode *prev;
  839.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  840.                    chain is kept, in case the node is the head
  841.                    of the chain and gets deleted. */
  842.   enum node_type type;        /* type of special token */
  843.   int length;            /* length of token, for quick comparison */
  844.   U_CHAR *name;            /* the actual name */
  845.   union hashval value;        /* pointer to expansion, or whatever */
  846. };
  847.  
  848. typedef struct hashnode HASHNODE;
  849.  
  850. /* Some definitions for the hash table.  The hash function MUST be
  851.    computed as shown in hashf () below.  That is because the rescan
  852.    loop computes the hash value `on the fly' for most tokens,
  853.    in order to avoid the overhead of a lot of procedure calls to
  854.    the hashf () function.  Hashf () only exists for the sake of
  855.    politeness, for use when speed isn't so important. */
  856.  
  857. #define HASHSIZE 1403
  858. static HASHNODE *hashtab[HASHSIZE];
  859. #define HASHSTEP(old, c) ((old << 2) + c)
  860. #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
  861.  
  862. /* Symbols to predefine.  */
  863.  
  864. #ifdef CPP_PREDEFINES
  865. static char *predefs = CPP_PREDEFINES;
  866. #else
  867. static char *predefs = "";
  868. #endif
  869.  
  870. /* We let tm.h override the types used here, to handle trivial differences
  871.    such as the choice of unsigned int or long unsigned int for size_t.
  872.    When machines start needing nontrivial differences in the size type,
  873.    it would be best to do something here to figure out automatically
  874.    from other information what type to use.  */
  875.  
  876. /* The string value for __SIZE_TYPE__.  */
  877.  
  878. #ifndef SIZE_TYPE
  879. #define SIZE_TYPE "long unsigned int"
  880. #endif
  881.  
  882. /* The string value for __PTRDIFF_TYPE__.  */
  883.  
  884. #ifndef PTRDIFF_TYPE
  885. #define PTRDIFF_TYPE "long int"
  886. #endif
  887.  
  888. /* The string value for __WCHAR_TYPE__.  */
  889.  
  890. #ifndef WCHAR_TYPE
  891. #define WCHAR_TYPE "int"
  892. #endif
  893. char * wchar_type = WCHAR_TYPE;
  894. #undef WCHAR_TYPE
  895.  
  896. /* The string value for __USER_LABEL_PREFIX__ */
  897.  
  898. #ifndef USER_LABEL_PREFIX
  899. #define USER_LABEL_PREFIX ""
  900. #endif
  901.  
  902. /* The string value for __REGISTER_PREFIX__ */
  903.  
  904. #ifndef REGISTER_PREFIX
  905. #define REGISTER_PREFIX ""
  906. #endif
  907.  
  908. /* The string value for __IMMEDIATE_PREFIX__ */
  909.  
  910. #ifndef IMMEDIATE_PREFIX
  911. #define IMMEDIATE_PREFIX ""
  912. #endif
  913.  
  914. /* In the definition of a #assert name, this structure forms
  915.    a list of the individual values asserted.
  916.    Each value is itself a list of "tokens".
  917.    These are strings that are compared by name.  */
  918.  
  919. struct tokenlist_list {
  920.   struct tokenlist_list *next;
  921.   struct arglist *tokens;
  922. };
  923.  
  924. struct assertion_hashnode {
  925.   struct assertion_hashnode *next;    /* double links for easy deletion */
  926.   struct assertion_hashnode *prev;
  927.   /* also, a back pointer to this node's hash
  928.      chain is kept, in case the node is the head
  929.      of the chain and gets deleted. */
  930.   struct assertion_hashnode **bucket_hdr;
  931.   int length;            /* length of token, for quick comparison */
  932.   U_CHAR *name;            /* the actual name */
  933.   /* List of token-sequences.  */
  934.   struct tokenlist_list *value;
  935. };
  936.  
  937. typedef struct assertion_hashnode ASSERTION_HASHNODE;
  938.  
  939. /* Some definitions for the hash table.  The hash function MUST be
  940.    computed as shown in hashf below.  That is because the rescan
  941.    loop computes the hash value `on the fly' for most tokens,
  942.    in order to avoid the overhead of a lot of procedure calls to
  943.    the hashf function.  hashf only exists for the sake of
  944.    politeness, for use when speed isn't so important. */
  945.  
  946. #define ASSERTION_HASHSIZE 37
  947. static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
  948.  
  949. /* Nonzero means inhibit macroexpansion of what seem to be
  950.    assertion tests, in rescan.  For #if.  */
  951. static int assertions_flag;
  952.  
  953. /* `struct directive' defines one #-directive, including how to handle it.  */
  954.  
  955. #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
  956.  
  957. struct directive {
  958.   int length;            /* Length of name */
  959.   int (*func) DO_PROTO;    /* Function to handle directive */
  960.   char *name;            /* Name of directive */
  961.   enum node_type type;        /* Code which describes which directive. */
  962.   char angle_brackets;        /* Nonzero => <...> is special.  */
  963.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  964.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  965. };
  966.  
  967. /* These functions are declared to return int instead of void since they
  968.    are going to be placed in the table and some old compilers have trouble with
  969.    pointers to functions returning void.  */
  970.  
  971. static int do_assert DO_PROTO;
  972. static int do_define DO_PROTO;
  973. static int do_elif DO_PROTO;
  974. static int do_else DO_PROTO;
  975. static int do_endif DO_PROTO;
  976. static int do_error DO_PROTO;
  977. static int do_ident DO_PROTO;
  978. static int do_if DO_PROTO;
  979. static int do_include DO_PROTO;
  980. static int do_line DO_PROTO;
  981. static int do_pragma DO_PROTO;
  982. #ifdef SCCS_DIRECTIVE
  983. static int do_sccs DO_PROTO;
  984. #endif
  985. static int do_unassert DO_PROTO;
  986. static int do_undef DO_PROTO;
  987. static int do_warning DO_PROTO;
  988. static int do_xifdef DO_PROTO;
  989.  
  990. /* Here is the actual list of #-directives, most-often-used first.  */
  991.  
  992. static struct directive directive_table[] = {
  993.   {  6, do_define, "define", T_DEFINE, 0, 1},
  994.   {  2, do_if, "if", T_IF},
  995.   {  5, do_xifdef, "ifdef", T_IFDEF},
  996.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  997.   {  5, do_endif, "endif", T_ENDIF},
  998.   {  4, do_else, "else", T_ELSE},
  999.   {  4, do_elif, "elif", T_ELIF},
  1000.   {  4, do_line, "line", T_LINE},
  1001.   {  7, do_include, "include", T_INCLUDE, 1},
  1002.   { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
  1003.   {  6, do_include, "import", T_IMPORT, 1},
  1004.   {  5, do_undef, "undef", T_UNDEF},
  1005.   {  5, do_error, "error", T_ERROR},
  1006.   {  7, do_warning, "warning", T_WARNING},
  1007. #ifdef SCCS_DIRECTIVE
  1008.   {  4, do_sccs, "sccs", T_SCCS},
  1009. #endif
  1010.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  1011.   {  5, do_ident, "ident", T_IDENT},
  1012.   {  6, do_assert, "assert", T_ASSERT},
  1013.   {  8, do_unassert, "unassert", T_UNASSERT},
  1014.   {  -1, 0, "", T_UNUSED},
  1015. };
  1016.  
  1017. /* When a directive handler is called,
  1018.    this points to the # (or the : of the %:) that started the directive.  */
  1019. U_CHAR *directive_start;
  1020.  
  1021. /* table to tell if char can be part of a C identifier. */
  1022. U_CHAR is_idchar[256];
  1023. /* table to tell if char can be first char of a c identifier. */
  1024. U_CHAR is_idstart[256];
  1025. /* table to tell if c is horizontal space.  */
  1026. U_CHAR is_hor_space[256];
  1027. /* table to tell if c is horizontal or vertical space.  */
  1028. static U_CHAR is_space[256];
  1029. /* names of some characters */
  1030. static char *char_name[256];
  1031.  
  1032. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  1033. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  1034.   
  1035. static int errors = 0;            /* Error counter for exit code */
  1036.  
  1037. /* Name of output file, for error messages.  */
  1038. static char *out_fname;
  1039.  
  1040. /* Zero means dollar signs are punctuation.
  1041.    -$ stores 0; -traditional may store 1.  Default is 1 for VMS, 0 otherwise.
  1042.    This must be 0 for correct processing of this ANSI C program:
  1043.     #define foo(a) #a
  1044.     #define lose(b) foo (b)
  1045.     #define test$
  1046.     lose (test)    */
  1047. static int dollars_in_ident;
  1048. #ifndef DOLLARS_IN_IDENTIFIERS
  1049. #define DOLLARS_IN_IDENTIFIERS 1
  1050. #endif
  1051.  
  1052.  
  1053. /* Stack of conditionals currently in progress
  1054.    (including both successful and failing conditionals).  */
  1055.  
  1056. struct if_stack {
  1057.   struct if_stack *next;    /* for chaining to the next stack frame */
  1058.   char *fname;        /* copied from input when frame is made */
  1059.   int lineno;            /* similarly */
  1060.   int if_succeeded;        /* true if a leg of this if-group
  1061.                     has been passed through rescan */
  1062.   U_CHAR *control_macro;    /* For #ifndef at start of file,
  1063.                    this is the macro name tested.  */
  1064.   enum node_type type;        /* type of last directive seen in this group */
  1065. };
  1066. typedef struct if_stack IF_STACK_FRAME;
  1067. static IF_STACK_FRAME *if_stack = NULL;
  1068.  
  1069. /* Buffer of -M output.  */
  1070. static char *deps_buffer;
  1071.  
  1072. /* Number of bytes allocated in above.  */
  1073. static int deps_allocated_size;
  1074.  
  1075. /* Number of bytes used.  */
  1076. static int deps_size;
  1077.  
  1078. /* Number of bytes since the last newline.  */
  1079. static int deps_column;
  1080.  
  1081. /* Nonzero means -I- has been seen,
  1082.    so don't look for #include "foo" the source-file directory.  */
  1083. static int ignore_srcdir;
  1084.  
  1085. static int safe_read PROTO((int, char *, int));
  1086. static void safe_write PROTO((int, char *, int));
  1087.  
  1088. int main PROTO((int, char **));
  1089.  
  1090. static void path_include PROTO((char *));
  1091.  
  1092. static U_CHAR *index0 PROTO((U_CHAR *, int, size_t));
  1093.  
  1094. static void trigraph_pcp PROTO((FILE_BUF *));
  1095.  
  1096. static void newline_fix PROTO((U_CHAR *));
  1097. static void name_newline_fix PROTO((U_CHAR *));
  1098.  
  1099. static char *get_lintcmd PROTO((U_CHAR *, U_CHAR *, U_CHAR **, int *, int *));
  1100.  
  1101. static void rescan PROTO((FILE_BUF *, int));
  1102.  
  1103. static FILE_BUF expand_to_temp_buffer PROTO((U_CHAR *, U_CHAR *, int, int));
  1104.  
  1105. static int handle_directive PROTO((FILE_BUF *, FILE_BUF *));
  1106.  
  1107. static struct tm *timestamp PROTO((void));
  1108. static void special_symbol PROTO((HASHNODE *, FILE_BUF *));
  1109.  
  1110. static int redundant_include_p PROTO((char *));
  1111. static is_system_include PROTO((char *));
  1112.  
  1113. static char *read_filename_string PROTO((int, FILE *));
  1114. static struct file_name_map *read_name_map PROTO((char *));
  1115. static int open_include_file PROTO((char *, struct file_name_list *));
  1116.  
  1117. static void finclude PROTO((int, char *, FILE_BUF *, int, struct file_name_list *));
  1118. static void record_control_macro PROTO((char *, U_CHAR *));
  1119.  
  1120. static int import_hash PROTO((char *));
  1121. static int lookup_import PROTO((char *, struct file_name_list *));
  1122. static void add_import PROTO((int, char *));
  1123.  
  1124. static char *check_precompiled PROTO((int, char *, char **));
  1125. static int check_preconditions PROTO((char *));
  1126. static void pcfinclude PROTO((U_CHAR *, U_CHAR *, U_CHAR *, FILE_BUF *));
  1127. static void pcstring_used PROTO((HASHNODE *));
  1128. static void write_output PROTO((void));
  1129. static void pass_thru_directive PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
  1130.  
  1131. static MACRODEF create_definition PROTO((U_CHAR *, U_CHAR *, FILE_BUF *));
  1132.  
  1133. static int check_macro_name PROTO((U_CHAR *, char *));
  1134. static int compare_defs PROTO((DEFINITION *, DEFINITION *));
  1135. static int comp_def_part PROTO((int, U_CHAR *, int, U_CHAR *, int, int));
  1136.  
  1137. static DEFINITION *collect_expansion  PROTO((U_CHAR *, U_CHAR *, int, struct arglist *));
  1138.  
  1139. int check_assertion PROTO((U_CHAR *, int, int, struct arglist *));
  1140. static int compare_token_lists PROTO((struct arglist *, struct arglist *));
  1141.  
  1142. static struct arglist *read_token_list PROTO((U_CHAR **, U_CHAR *, int *));
  1143. static void free_token_list PROTO((struct arglist *));
  1144.  
  1145. static ASSERTION_HASHNODE *assertion_install PROTO((U_CHAR *, int, int));
  1146. static ASSERTION_HASHNODE *assertion_lookup PROTO((U_CHAR *, int, int));
  1147. static void delete_assertion PROTO((ASSERTION_HASHNODE *));
  1148.  
  1149. static void do_once PROTO((void));
  1150.  
  1151. static HOST_WIDE_INT eval_if_expression PROTO((U_CHAR *, int));
  1152. static void conditional_skip PROTO((FILE_BUF *, int, enum node_type, U_CHAR *, FILE_BUF *));
  1153. static void skip_if_group PROTO((FILE_BUF *, int, FILE_BUF *));
  1154. static void validate_else PROTO((U_CHAR *));
  1155.  
  1156. static U_CHAR *skip_to_end_of_comment PROTO((FILE_BUF *, int *, int));
  1157. static U_CHAR *skip_quoted_string PROTO((U_CHAR *, U_CHAR *, int, int *, int *, int *));
  1158. static char *quote_string PROTO((char *, char *));
  1159. static U_CHAR *skip_paren_group PROTO((FILE_BUF *));
  1160.  
  1161. /* Last arg to output_line_directive.  */
  1162. enum file_change_code {same_file, enter_file, leave_file};
  1163. static void output_line_directive PROTO((FILE_BUF *, FILE_BUF *, int, enum file_change_code));
  1164.  
  1165. static void macroexpand PROTO((HASHNODE *, FILE_BUF *));
  1166.  
  1167. struct argdata;
  1168. static char *macarg PROTO((struct argdata *, int));
  1169.  
  1170. static U_CHAR *macarg1 PROTO((U_CHAR *, U_CHAR *, int *, int *, int *, int));
  1171.  
  1172. static int discard_comments PROTO((U_CHAR *, int, int));
  1173.  
  1174. static int change_newlines PROTO((U_CHAR *, int));
  1175.  
  1176. char *my_strerror PROTO((int));
  1177. void error PRINTF_PROTO_1((char *, ...));
  1178. static void verror PROTO((char *, va_list));
  1179. static void error_from_errno PROTO((char *));
  1180. void warning PRINTF_PROTO_1((char *, ...));
  1181. static void vwarning PROTO((char *, va_list));
  1182. static void error_with_line PRINTF_PROTO_2((int, char *, ...));
  1183. static void verror_with_line PROTO((int, char *, va_list));
  1184. static void vwarning_with_line PROTO((int, char *, va_list));
  1185. void pedwarn PRINTF_PROTO_1((char *, ...));
  1186. void pedwarn_with_line PRINTF_PROTO_2((int, char *, ...));
  1187. static void pedwarn_with_file_and_line PRINTF_PROTO_3((char *, int, char *, ...));
  1188.  
  1189. static void print_containing_files PROTO((void));
  1190.  
  1191. static int line_for_error PROTO((int));
  1192. static int grow_outbuf PROTO((FILE_BUF *, int));
  1193.  
  1194. static HASHNODE *install PROTO((U_CHAR *, int, enum node_type, char *, int));
  1195. HASHNODE *lookup PROTO((U_CHAR *, int, int));
  1196. static void delete_macro PROTO((HASHNODE *));
  1197. static int hashf PROTO((U_CHAR *, int, int));
  1198.  
  1199. static void dump_single_macro PROTO((HASHNODE *, FILE *));
  1200. static void dump_all_macros PROTO((void));
  1201. static void dump_defn_1 PROTO((U_CHAR *, int, int, FILE *));
  1202. static void dump_arg_n PROTO((DEFINITION *, int, FILE *));
  1203.  
  1204. static void initialize_char_syntax PROTO((void));
  1205. static void initialize_builtins PROTO((FILE_BUF *, FILE_BUF *));
  1206.  
  1207. static void make_definition PROTO((char *, FILE_BUF *));
  1208. static void make_undef PROTO((char *, FILE_BUF *));
  1209.  
  1210. static void make_assertion PROTO((char *, char *));
  1211.  
  1212. static void append_include_chain PROTO((struct file_name_list *, struct file_name_list *));
  1213.  
  1214. static void deps_output PROTO((char *, int));
  1215.  
  1216. static void fatal PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn));
  1217. void fancy_abort PROTO((void)) __attribute__ ((noreturn));
  1218. static void perror_with_name PROTO((char *));
  1219. static void pfatal_with_name PROTO((char *)) __attribute__ ((noreturn));
  1220. static void pipe_closed PROTO((int)) __attribute__ ((noreturn));
  1221.  
  1222. static void memory_full PROTO((void)) __attribute__ ((noreturn));
  1223. GENERIC_PTR xmalloc PROTO((size_t));
  1224. static GENERIC_PTR xrealloc PROTO((GENERIC_PTR, size_t));
  1225. static GENERIC_PTR xcalloc PROTO((size_t, size_t));
  1226. static char *savestring PROTO((char *));
  1227.  
  1228. static int file_size_and_mode PROTO((int, int *, long int *));
  1229. static void output_dots PROTO((FILE *, int));
  1230.  
  1231. /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
  1232.    retrying if necessary.  Return a negative value if an error occurs,
  1233.    otherwise return the actual number of bytes read,
  1234.    which must be LEN unless end-of-file was reached.  */
  1235.  
  1236. static int
  1237. safe_read (desc, ptr, len)
  1238.      int desc;
  1239.      char *ptr;
  1240.      int len;
  1241. {
  1242.   int left = len;
  1243.   while (left > 0) {
  1244.     int nchars = read (desc, ptr, left);
  1245.     if (nchars < 0)
  1246.       {
  1247. #ifdef EINTR
  1248.     if (errno == EINTR)
  1249.       continue;
  1250. #endif
  1251.     return nchars;
  1252.       }
  1253.     if (nchars == 0)
  1254.       break;
  1255.     ptr += nchars;
  1256.     left -= nchars;
  1257.   }
  1258.   return len - left;
  1259. }
  1260.  
  1261. /* Write LEN bytes at PTR to descriptor DESC,
  1262.    retrying if necessary, and treating any real error as fatal.  */
  1263.  
  1264. static void
  1265. safe_write (desc, ptr, len)
  1266.      int desc;
  1267.      char *ptr;
  1268.      int len;
  1269. {
  1270.   while (len > 0) {
  1271.     int written = write (desc, ptr, len);
  1272.     if (written < 0)
  1273.       {
  1274. #ifdef EINTR
  1275.     if (errno == EINTR)
  1276.       continue;
  1277. #endif
  1278.     pfatal_with_name (out_fname);
  1279.       }
  1280.     ptr += written;
  1281.     len -= written;
  1282.   }
  1283. }
  1284.  
  1285. int
  1286. main (argc, argv)
  1287.      int argc;
  1288.      char **argv;
  1289. {
  1290.   int st_mode;
  1291.   long st_size;
  1292.   char *in_fname;
  1293.   char *cp;
  1294.   int f, i;
  1295.   FILE_BUF *fp;
  1296.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  1297.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  1298.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  1299.   char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
  1300.   char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
  1301.  
  1302.   /* Record the option used with each element of pend_assertions.
  1303.      This is preparation for supporting more than one option for making
  1304.      an assertion.  */
  1305.   char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
  1306.   int inhibit_predefs = 0;
  1307.   int no_standard_includes = 0;
  1308.   int no_standard_cplusplus_includes = 0;
  1309.   int missing_newline = 0;
  1310.  
  1311.   /* Non-0 means don't output the preprocessed program.  */
  1312.   int inhibit_output = 0;
  1313.   /* Non-0 means -v, so print the full set of include dirs.  */
  1314.   int verbose = 0;
  1315.  
  1316.   /* File name which deps are being written to.
  1317.      This is 0 if deps are being written to stdout.  */
  1318.   char *deps_file = 0;
  1319.   /* Fopen file mode to open deps_file with.  */
  1320.   char *deps_mode = "a";
  1321.   /* Stream on which to print the dependency information.  */
  1322.   FILE *deps_stream = 0;
  1323.   /* Target-name to write with the dependency information.  */
  1324.   char *deps_target = 0;
  1325.  
  1326. #ifdef RLIMIT_STACK
  1327.   /* Get rid of any avoidable limit on stack size.  */
  1328.   {
  1329.     struct rlimit rlim;
  1330.  
  1331.     /* Set the stack limit huge so that alloca (particularly stringtab
  1332.      * in dbxread.c) does not fail. */
  1333.     getrlimit (RLIMIT_STACK, &rlim);
  1334.     rlim.rlim_cur = rlim.rlim_max;
  1335.     setrlimit (RLIMIT_STACK, &rlim);
  1336.   }
  1337. #endif /* RLIMIT_STACK defined */
  1338.  
  1339. #ifdef __amigados__
  1340.   {
  1341.     char *envstr;
  1342.  
  1343.     if (envstr = getenv("GCCPRIORITY"))
  1344.       if (((i = atoi(envstr)) > -20) && (i < 20)) amiga_priority = i;
  1345.   }
  1346. #endif /* __amigados__ */
  1347.  
  1348. #ifdef SIGPIPE
  1349.   signal (SIGPIPE, pipe_closed);
  1350. #endif
  1351.  
  1352.   cp = argv[0] + strlen (argv[0]);
  1353.   while (cp != argv[0] && cp[-1] != '/'
  1354. #ifdef DIR_SEPARATOR
  1355.      && cp[-1] != DIR_SEPARATOR
  1356. #endif
  1357. #ifdef VOL_SEPARATOR
  1358.      && cp[-1] != VOL_SEPARATOR
  1359. #endif
  1360.      )
  1361.     --cp;
  1362.   progname = cp;
  1363.  
  1364. #ifdef VMS
  1365.   {
  1366.     /* Remove directories from PROGNAME.  */
  1367.     char *p;
  1368.     char *s = progname;
  1369.  
  1370.     if ((p = rindex (s, ':')) != 0) s = p + 1;    /* skip device */
  1371.     if ((p = rindex (s, ']')) != 0) s = p + 1;    /* skip directory */
  1372.     if ((p = rindex (s, '>')) != 0) s = p + 1;    /* alternate (int'n'l) dir */
  1373.     s = progname = savestring (s);
  1374.     if ((p = rindex (s, ';')) != 0) *p = '\0';    /* strip version number */
  1375.     if ((p = rindex (s, '.')) != 0        /* strip type iff ".exe" */
  1376.     && (p[1] == 'e' || p[1] == 'E')
  1377.     && (p[2] == 'x' || p[2] == 'X')
  1378.     && (p[3] == 'e' || p[3] == 'E')
  1379.     && !p[4])
  1380.       *p = '\0';
  1381.   }
  1382. #endif
  1383.  
  1384.   in_fname = NULL;
  1385.   out_fname = NULL;
  1386.  
  1387.   /* Initialize is_idchar to allow $.  */
  1388.   dollars_in_ident = 1;
  1389.   initialize_char_syntax ();
  1390.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
  1391.  
  1392.   no_line_directives = 0;
  1393.   no_trigraphs = 1;
  1394.   dump_macros = dump_none;
  1395.   no_output = 0;
  1396.   cplusplus = 0;
  1397.   cplusplus_comments = 0;
  1398.  
  1399.   bzero ((char *) pend_files, argc * sizeof (char *));
  1400.   bzero ((char *) pend_defs, argc * sizeof (char *));
  1401.   bzero ((char *) pend_undefs, argc * sizeof (char *));
  1402.   bzero ((char *) pend_assertions, argc * sizeof (char *));
  1403.   bzero ((char *) pend_includes, argc * sizeof (char *));
  1404.  
  1405.   /* Process switches and find input file name.  */
  1406.  
  1407.   for (i = 1; i < argc; i++) {
  1408.     if (argv[i][0] != '-') {
  1409.       if (out_fname != NULL)
  1410.     fatal ("Usage: %s [switches] input output", argv[0]);
  1411.       else if (in_fname != NULL)
  1412.     out_fname = argv[i];
  1413.       else
  1414.     in_fname = argv[i];
  1415.     } else {
  1416.       switch (argv[i][1]) {
  1417.  
  1418.       case 'i':
  1419.     if (!strcmp (argv[i], "-include")) {
  1420.       if (i + 1 == argc)
  1421.         fatal ("Filename missing after `-include' option");
  1422.       else
  1423.         pend_includes[i] = argv[i+1], i++;
  1424.     }
  1425.     if (!strcmp (argv[i], "-imacros")) {
  1426.       if (i + 1 == argc)
  1427.         fatal ("Filename missing after `-imacros' option");
  1428.       else
  1429.         pend_files[i] = argv[i+1], i++;
  1430.     }
  1431.     if (!strcmp (argv[i], "-iprefix")) {
  1432.       if (i + 1 == argc)
  1433.         fatal ("Filename missing after `-iprefix' option");
  1434.       else
  1435.         include_prefix = argv[++i];
  1436.     }
  1437.     if (!strcmp (argv[i], "-ifoutput")) {
  1438.       output_conditionals = 1;
  1439.     }
  1440.     if (!strcmp (argv[i], "-isystem")) {
  1441.       struct file_name_list *dirtmp;
  1442.  
  1443.       if (i + 1 == argc)
  1444.         fatal ("Filename missing after `-isystem' option");
  1445.  
  1446.       dirtmp = (struct file_name_list *)
  1447.         xmalloc (sizeof (struct file_name_list));
  1448.       dirtmp->next = 0;
  1449.       dirtmp->control_macro = 0;
  1450.       dirtmp->c_system_include_path = 1;
  1451.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + 1);
  1452.       strcpy (dirtmp->fname, argv[++i]);
  1453.       dirtmp->got_name_map = 0;
  1454.  
  1455.       if (before_system == 0)
  1456.         before_system = dirtmp;
  1457.       else
  1458.         last_before_system->next = dirtmp;
  1459.       last_before_system = dirtmp; /* Tail follows the last one */
  1460.     }
  1461.     /* Add directory to end of path for includes,
  1462.        with the default prefix at the front of its name.  */
  1463.     if (!strcmp (argv[i], "-iwithprefix")) {
  1464.       struct file_name_list *dirtmp;
  1465.       char *prefix;
  1466.  
  1467.       if (include_prefix != 0)
  1468.         prefix = include_prefix;
  1469.       else {
  1470.         prefix = savestring (GCC_INCLUDE_DIR);
  1471.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1472.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1473.           prefix[strlen (prefix) - 7] = 0;
  1474.       }
  1475.  
  1476.       dirtmp = (struct file_name_list *)
  1477.         xmalloc (sizeof (struct file_name_list));
  1478.       dirtmp->next = 0;    /* New one goes on the end */
  1479.       dirtmp->control_macro = 0;
  1480.       dirtmp->c_system_include_path = 0;
  1481.       if (i + 1 == argc)
  1482.         fatal ("Directory name missing after `-iwithprefix' option");
  1483.  
  1484.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + strlen (prefix) + 1);
  1485.       strcpy (dirtmp->fname, prefix);
  1486.       strcat (dirtmp->fname, argv[++i]);
  1487.       dirtmp->got_name_map = 0;
  1488.  
  1489.       if (after_include == 0)
  1490.         after_include = dirtmp;
  1491.       else
  1492.         last_after_include->next = dirtmp;
  1493.       last_after_include = dirtmp; /* Tail follows the last one */
  1494.     }
  1495.     /* Add directory to main path for includes,
  1496.        with the default prefix at the front of its name.  */
  1497.     if (!strcmp (argv[i], "-iwithprefixbefore")) {
  1498.       struct file_name_list *dirtmp;
  1499.       char *prefix;
  1500.  
  1501.       if (include_prefix != 0)
  1502.         prefix = include_prefix;
  1503.       else {
  1504.         prefix = savestring (GCC_INCLUDE_DIR);
  1505.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1506.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1507.           prefix[strlen (prefix) - 7] = 0;
  1508.       }
  1509.  
  1510.       dirtmp = (struct file_name_list *)
  1511.         xmalloc (sizeof (struct file_name_list));
  1512.       dirtmp->next = 0;    /* New one goes on the end */
  1513.       dirtmp->control_macro = 0;
  1514.       dirtmp->c_system_include_path = 0;
  1515.       if (i + 1 == argc)
  1516.         fatal ("Directory name missing after `-iwithprefixbefore' option");
  1517.  
  1518.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + strlen (prefix) + 1);
  1519.       strcpy (dirtmp->fname, prefix);
  1520.       strcat (dirtmp->fname, argv[++i]);
  1521.       dirtmp->got_name_map = 0;
  1522.  
  1523.       append_include_chain (dirtmp, dirtmp);
  1524.     }
  1525.     /* Add directory to end of path for includes.  */
  1526.     if (!strcmp (argv[i], "-idirafter")) {
  1527.       struct file_name_list *dirtmp;
  1528.  
  1529.       dirtmp = (struct file_name_list *)
  1530.         xmalloc (sizeof (struct file_name_list));
  1531.       dirtmp->next = 0;    /* New one goes on the end */
  1532.       dirtmp->control_macro = 0;
  1533.       dirtmp->c_system_include_path = 0;
  1534.       if (i + 1 == argc)
  1535.         fatal ("Directory name missing after `-idirafter' option");
  1536.       else
  1537.         dirtmp->fname = argv[++i];
  1538.       dirtmp->got_name_map = 0;
  1539.  
  1540.       if (after_include == 0)
  1541.         after_include = dirtmp;
  1542.       else
  1543.         last_after_include->next = dirtmp;
  1544.       last_after_include = dirtmp; /* Tail follows the last one */
  1545.     }
  1546.     break;
  1547.  
  1548.       case 'o':
  1549.     if (out_fname != NULL)
  1550.       fatal ("Output filename specified twice");
  1551.     if (i + 1 == argc)
  1552.       fatal ("Filename missing after -o option");
  1553.     out_fname = argv[++i];
  1554.     if (!strcmp (out_fname, "-"))
  1555.       out_fname = "";
  1556.     break;
  1557.  
  1558.       case 'p':
  1559. #ifdef __amigados__
  1560.     if (!strcmp (argv[i], "-priority")) {
  1561.       if (i + 1 == argc)
  1562.         fatal ("Priority missing after -P option");
  1563.           if ((amiga_priority = atoi(argv[++i])) < -25)
  1564.         amiga_priority = -25;
  1565.           else if (amiga_priority > 25)
  1566.         amiga_priority = 25;
  1567.         } else
  1568. #endif /* __amigados__ */
  1569.     if (!strcmp (argv[i], "-pedantic"))
  1570.       pedantic = 1;
  1571.     else if (!strcmp (argv[i], "-pedantic-errors")) {
  1572.       pedantic = 1;
  1573.       pedantic_errors = 1;
  1574.     } else if (!strcmp (argv[i], "-pcp")) {
  1575.       char *pcp_fname;
  1576.       if (i + 1 == argc)
  1577.         fatal ("Filename missing after -pcp option");
  1578.       pcp_fname = argv[++i];
  1579.       pcp_outfile = 
  1580.         ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
  1581.          ? fopen (pcp_fname, "w")
  1582.          : stdout);
  1583.       if (pcp_outfile == 0)
  1584.         pfatal_with_name (pcp_fname);
  1585.       no_precomp = 1;
  1586.     }
  1587.     break;
  1588.  
  1589.       case 't':
  1590.     if (!strcmp (argv[i], "-traditional")) {
  1591.       traditional = 1;
  1592.       if (dollars_in_ident > 0)
  1593.         dollars_in_ident = 1;
  1594.     } else if (!strcmp (argv[i], "-trigraphs")) {
  1595.       no_trigraphs = 0;
  1596.     }
  1597.     break;
  1598.  
  1599.       case 'l':
  1600.     if (! strcmp (argv[i], "-lang-c"))
  1601.       cplusplus = 0, cplusplus_comments = 0, objc = 0;
  1602.     if (! strcmp (argv[i], "-lang-c++"))
  1603.       cplusplus = 1, cplusplus_comments = 1, objc = 0;
  1604.     if (! strcmp (argv[i], "-lang-c-c++-comments"))
  1605.       cplusplus = 0, cplusplus_comments = 1, objc = 0;
  1606.     if (! strcmp (argv[i], "-lang-objc"))
  1607.       objc = 1, cplusplus = 0, cplusplus_comments = 1;
  1608.     if (! strcmp (argv[i], "-lang-objc++"))
  1609.       objc = 1, cplusplus = 1, cplusplus_comments = 1;
  1610.      if (! strcmp (argv[i], "-lang-asm"))
  1611.        lang_asm = 1;
  1612.      if (! strcmp (argv[i], "-lint"))
  1613.        for_lint = 1;
  1614.     break;
  1615.  
  1616.       case '+':
  1617.     cplusplus = 1, cplusplus_comments = 1;
  1618.     break;
  1619.  
  1620.       case 'w':
  1621.     inhibit_warnings = 1;
  1622.     break;
  1623.  
  1624.       case 'W':
  1625.     if (!strcmp (argv[i], "-Wtrigraphs"))
  1626.       warn_trigraphs = 1;
  1627.     else if (!strcmp (argv[i], "-Wno-trigraphs"))
  1628.       warn_trigraphs = 0;
  1629.     else if (!strcmp (argv[i], "-Wcomment"))
  1630.       warn_comments = 1;
  1631.     else if (!strcmp (argv[i], "-Wno-comment"))
  1632.       warn_comments = 0;
  1633.     else if (!strcmp (argv[i], "-Wcomments"))
  1634.       warn_comments = 1;
  1635.     else if (!strcmp (argv[i], "-Wno-comments"))
  1636.       warn_comments = 0;
  1637.     else if (!strcmp (argv[i], "-Wtraditional"))
  1638.       warn_stringify = 1;
  1639.     else if (!strcmp (argv[i], "-Wno-traditional"))
  1640.       warn_stringify = 0;
  1641.     else if (!strcmp (argv[i], "-Wimport"))
  1642.       warn_import = 1;
  1643.     else if (!strcmp (argv[i], "-Wno-import"))
  1644.       warn_import = 0;
  1645.     else if (!strcmp (argv[i], "-Werror"))
  1646.       warnings_are_errors = 1;
  1647.     else if (!strcmp (argv[i], "-Wno-error"))
  1648.       warnings_are_errors = 0;
  1649.     else if (!strcmp (argv[i], "-Wall"))
  1650.       {
  1651.         warn_trigraphs = 1;
  1652.         warn_comments = 1;
  1653.       }
  1654.     break;
  1655.  
  1656.       case 'M':
  1657.     /* The style of the choices here is a bit mixed.
  1658.        The chosen scheme is a hybrid of keeping all options in one string
  1659.        and specifying each option in a separate argument:
  1660.        -M|-MM|-MD file|-MMD file [-MG].  An alternative is:
  1661.        -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
  1662.        -M[M][G][D file].  This is awkward to handle in specs, and is not
  1663.        as extensible.  */
  1664.     /* ??? -MG must be specified in addition to one of -M or -MM.
  1665.        This can be relaxed in the future without breaking anything.
  1666.        The converse isn't true.  */
  1667.  
  1668.     /* -MG isn't valid with -MD or -MMD.  This is checked for later.  */
  1669.     if (!strcmp (argv[i], "-MG"))
  1670.       {
  1671.         print_deps_missing_files = 1;
  1672.         break;
  1673.       }
  1674.     if (!strcmp (argv[i], "-M"))
  1675.       print_deps = 2;
  1676.     else if (!strcmp (argv[i], "-MM"))
  1677.       print_deps = 1;
  1678.     else if (!strcmp (argv[i], "-MD"))
  1679.       print_deps = 2;
  1680.     else if (!strcmp (argv[i], "-MMD"))
  1681.       print_deps = 1;
  1682.     /* For -MD and -MMD options, write deps on file named by next arg.  */
  1683.     if (!strcmp (argv[i], "-MD")
  1684.         || !strcmp (argv[i], "-MMD")) {
  1685.       if (i + 1 == argc)
  1686.         fatal ("Filename missing after %s option", argv[i]);
  1687.       i++;
  1688.       deps_file = argv[i];
  1689.       deps_mode = "w";
  1690.     } else {
  1691.       /* For -M and -MM, write deps on standard output
  1692.          and suppress the usual output.  */
  1693.       deps_stream = stdout;
  1694.       inhibit_output = 1;
  1695.     }      
  1696.     break;
  1697.  
  1698.       case 'd':
  1699.     {
  1700.       char *p = argv[i] + 2;
  1701.       char c;
  1702.       while ((c = *p++)) {
  1703.         /* Arg to -d specifies what parts of macros to dump */
  1704.         switch (c) {
  1705.         case 'M':
  1706.           dump_macros = dump_only;
  1707.           no_output = 1;
  1708.           break;
  1709.         case 'N':
  1710.           dump_macros = dump_names;
  1711.           break;
  1712.         case 'D':
  1713.           dump_macros = dump_definitions;
  1714.           break;
  1715.         }
  1716.       }
  1717.     }
  1718.     break;
  1719.  
  1720.       case 'g':
  1721.     if (argv[i][2] == '3')
  1722.       debug_output = 1;
  1723.     break;
  1724.  
  1725.       case 'v':
  1726.     fprintf (stderr, "GNU CPP version %s", version_string);
  1727. #ifdef TARGET_VERSION
  1728.     TARGET_VERSION;
  1729. #endif
  1730.     fprintf (stderr, "\n");
  1731.     verbose = 1;
  1732.     break;
  1733.  
  1734.       case 'H':
  1735.     print_include_names = 1;
  1736.     break;
  1737.  
  1738.       case 'D':
  1739.     if (argv[i][2] != 0)
  1740.       pend_defs[i] = argv[i] + 2;
  1741.     else if (i + 1 == argc)
  1742.       fatal ("Macro name missing after -D option");
  1743.     else
  1744.       i++, pend_defs[i] = argv[i];
  1745.     break;
  1746.  
  1747.       case 'A':
  1748.     {
  1749.       char *p;
  1750.  
  1751.       if (argv[i][2] != 0)
  1752.         p = argv[i] + 2;
  1753.       else if (i + 1 == argc)
  1754.         fatal ("Assertion missing after -A option");
  1755.       else
  1756.         p = argv[++i];
  1757.  
  1758.       if (!strcmp (p, "-")) {
  1759.         /* -A- eliminates all predefined macros and assertions.
  1760.            Let's include also any that were specified earlier
  1761.            on the command line.  That way we can get rid of any
  1762.            that were passed automatically in from GCC.  */
  1763.         int j;
  1764.         inhibit_predefs = 1;
  1765.         for (j = 0; j < i; j++)
  1766.           pend_defs[j] = pend_assertions[j] = 0;
  1767.       } else {
  1768.         pend_assertions[i] = p;
  1769.         pend_assertion_options[i] = "-A";
  1770.       }
  1771.     }
  1772.     break;
  1773.  
  1774.       case 'U':        /* JF #undef something */
  1775.     if (argv[i][2] != 0)
  1776.       pend_undefs[i] = argv[i] + 2;
  1777.     else if (i + 1 == argc)
  1778.       fatal ("Macro name missing after -U option");
  1779.     else
  1780.       pend_undefs[i] = argv[i+1], i++;
  1781.     break;
  1782.  
  1783.       case 'C':
  1784.     put_out_comments = 1;
  1785.     break;
  1786.  
  1787.       case 'E':            /* -E comes from cc -E; ignore it.  */
  1788.     break;
  1789.  
  1790.       case 'P':
  1791.     no_line_directives = 1;
  1792.     break;
  1793.  
  1794.       case '$':            /* Don't include $ in identifiers.  */
  1795.     dollars_in_ident = 0;
  1796.     break;
  1797.  
  1798.       case 'I':            /* Add directory to path for includes.  */
  1799.     {
  1800.       struct file_name_list *dirtmp;
  1801.  
  1802.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1803.         ignore_srcdir = 1;
  1804.         /* Don't use any preceding -I directories for #include <...>.  */
  1805.         first_bracket_include = 0;
  1806.       }
  1807.       else {
  1808.         dirtmp = (struct file_name_list *)
  1809.           xmalloc (sizeof (struct file_name_list));
  1810.         dirtmp->next = 0;        /* New one goes on the end */
  1811.         dirtmp->control_macro = 0;
  1812.         dirtmp->c_system_include_path = 0;
  1813.         if (argv[i][2] != 0)
  1814.           dirtmp->fname = argv[i] + 2;
  1815.         else if (i + 1 == argc)
  1816.           fatal ("Directory name missing after -I option");
  1817.         else
  1818.           dirtmp->fname = argv[++i];
  1819.         dirtmp->got_name_map = 0;
  1820.         append_include_chain (dirtmp, dirtmp);
  1821.       }
  1822.     }
  1823.     break;
  1824.  
  1825.       case 'n':
  1826.     if (!strcmp (argv[i], "-nostdinc"))
  1827.       /* -nostdinc causes no default include directories.
  1828.          You must specify all include-file directories with -I.  */
  1829.       no_standard_includes = 1;
  1830.     else if (!strcmp (argv[i], "-nostdinc++"))
  1831.       /* -nostdinc++ causes no default C++-specific include directories. */
  1832.       no_standard_cplusplus_includes = 1;
  1833.     else if (!strcmp (argv[i], "-noprecomp"))
  1834.       no_precomp = 1;
  1835.     break;
  1836.  
  1837.       case 'u':
  1838.     /* Sun compiler passes undocumented switch "-undef".
  1839.        Let's assume it means to inhibit the predefined symbols.  */
  1840.     inhibit_predefs = 1;
  1841.     break;
  1842.  
  1843.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  1844.     if (in_fname == NULL) {
  1845.       in_fname = "";
  1846.       break;
  1847.     } else if (out_fname == NULL) {
  1848.       out_fname = "";
  1849.       break;
  1850.     }    /* else fall through into error */
  1851.  
  1852.       default:
  1853.     fatal ("Invalid option `%s'", argv[i]);
  1854.       }
  1855.     }
  1856.   }
  1857.  
  1858. #ifdef __amigados__
  1859.   Forbid();
  1860.   amiga_task = FindTask(NULL);
  1861.   Permit();
  1862.   amiga_old_priority = SetTaskPri(amiga_task, amiga_priority);
  1863. #endif /* __amigados__ */
  1864.  
  1865.   /* Add dirs from CPATH after dirs from -I.  */
  1866.   /* There seems to be confusion about what CPATH should do,
  1867.      so for the moment it is not documented.  */
  1868.   /* Some people say that CPATH should replace the standard include dirs,
  1869.      but that seems pointless: it comes before them, so it overrides them
  1870.      anyway.  */
  1871.   cp = getenv ("CPATH");
  1872.   if (cp && ! no_standard_includes)
  1873.     path_include (cp);
  1874.  
  1875.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  1876.   initialize_char_syntax ();
  1877.  
  1878.   /* Initialize output buffer */
  1879.  
  1880.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  1881.   outbuf.bufp = outbuf.buf;
  1882.   outbuf.length = OUTBUF_SIZE;
  1883.  
  1884.   /* Do partial setup of input buffer for the sake of generating
  1885.      early #line directives (when -g is in effect).  */
  1886.  
  1887.   fp = &instack[++indepth];
  1888.   if (in_fname == NULL)
  1889.     in_fname = "";
  1890.   fp->nominal_fname = fp->fname = in_fname;
  1891.   fp->lineno = 0;
  1892.  
  1893.   /* In C++, wchar_t is a distinct basic type, and we can expect
  1894.      __wchar_t to be defined by cc1plus.  */
  1895.   if (cplusplus)
  1896.     wchar_type = "__wchar_t";
  1897.  
  1898.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  1899.      and option processing.  */
  1900.   initialize_builtins (fp, &outbuf);
  1901.  
  1902.   /* Do standard #defines and assertions
  1903.      that identify system and machine type.  */
  1904.  
  1905.   if (!inhibit_predefs) {
  1906.     char *p = (char *) alloca (strlen (predefs) + 1);
  1907.     strcpy (p, predefs);
  1908.     while (*p) {
  1909.       char *q;
  1910.       while (*p == ' ' || *p == '\t')
  1911.     p++;
  1912.       /* Handle -D options.  */ 
  1913.       if (p[0] == '-' && p[1] == 'D') {
  1914.     q = &p[2];
  1915.     while (*p && *p != ' ' && *p != '\t')
  1916.       p++;
  1917.     if (*p != 0)
  1918.       *p++= 0;
  1919.     if (debug_output)
  1920.       output_line_directive (fp, &outbuf, 0, same_file);
  1921.     make_definition (q, &outbuf);
  1922.     while (*p == ' ' || *p == '\t')
  1923.       p++;
  1924.       } else if (p[0] == '-' && p[1] == 'A') {
  1925.     /* Handle -A options (assertions).  */ 
  1926.     char *assertion;
  1927.     char *past_name;
  1928.     char *value;
  1929.     char *past_value;
  1930.     char *termination;
  1931.     int save_char;
  1932.  
  1933.     assertion = &p[2];
  1934.     past_name = assertion;
  1935.     /* Locate end of name.  */
  1936.     while (*past_name && *past_name != ' '
  1937.            && *past_name != '\t' && *past_name != '(')
  1938.       past_name++;
  1939.     /* Locate `(' at start of value.  */
  1940.     value = past_name;
  1941.     while (*value && (*value == ' ' || *value == '\t'))
  1942.       value++;
  1943.     if (*value++ != '(')
  1944.       abort ();
  1945.     while (*value && (*value == ' ' || *value == '\t'))
  1946.       value++;
  1947.     past_value = value;
  1948.     /* Locate end of value.  */
  1949.     while (*past_value && *past_value != ' '
  1950.            && *past_value != '\t' && *past_value != ')')
  1951.       past_value++;
  1952.     termination = past_value;
  1953.     while (*termination && (*termination == ' ' || *termination == '\t'))
  1954.       termination++;
  1955.     if (*termination++ != ')')
  1956.       abort ();
  1957.     if (*termination && *termination != ' ' && *termination != '\t')
  1958.       abort ();
  1959.     /* Temporarily null-terminate the value.  */
  1960.     save_char = *termination;
  1961.     *termination = '\0';
  1962.     /* Install the assertion.  */
  1963.     make_assertion ("-A", assertion);
  1964.     *termination = (char) save_char;
  1965.     p = termination;
  1966.     while (*p == ' ' || *p == '\t')
  1967.       p++;
  1968.       } else {
  1969.     abort ();
  1970.       }
  1971.     }
  1972.   }
  1973.  
  1974.   /* Now handle the command line options.  */
  1975.  
  1976.   /* Do -U's, -D's and -A's in the order they were seen.  */
  1977.   for (i = 1; i < argc; i++) {
  1978.     if (pend_undefs[i]) {
  1979.       if (debug_output)
  1980.         output_line_directive (fp, &outbuf, 0, same_file);
  1981.       make_undef (pend_undefs[i], &outbuf);
  1982.     }
  1983.     if (pend_defs[i]) {
  1984.       if (debug_output)
  1985.         output_line_directive (fp, &outbuf, 0, same_file);
  1986.       make_definition (pend_defs[i], &outbuf);
  1987.     }
  1988.     if (pend_assertions[i])
  1989.       make_assertion (pend_assertion_options[i], pend_assertions[i]);
  1990.   }
  1991.  
  1992.   done_initializing = 1;
  1993.  
  1994.   { /* read the appropriate environment variable and if it exists
  1995.        replace include_defaults with the listed path. */
  1996.     char *epath = 0;
  1997.     switch ((objc << 1) + cplusplus)
  1998.       {
  1999.       case 0:
  2000.     epath = getenv ("C_INCLUDE_PATH");
  2001.     break;
  2002.       case 1:
  2003.     epath = getenv ("CPLUS_INCLUDE_PATH");
  2004.     break;
  2005.       case 2:
  2006.     epath = getenv ("OBJC_INCLUDE_PATH");
  2007.     break;
  2008.       case 3:
  2009.     epath = getenv ("OBJCPLUS_INCLUDE_PATH");
  2010.     break;
  2011.       }
  2012.     /* If the environment var for this language is set,
  2013.        add to the default list of include directories.  */
  2014.     if (epath) {
  2015.       char *nstore = (char *) alloca (strlen (epath) + 2);
  2016.       int num_dirs;
  2017.       char *startp, *endp;
  2018.  
  2019.       for (num_dirs = 1, startp = epath; *startp; startp++)
  2020.     if (*startp == PATH_SEPARATOR)
  2021.       num_dirs++;
  2022.       include_defaults
  2023.     = (struct default_include *) xmalloc ((num_dirs
  2024.                            * sizeof (struct default_include))
  2025.                           + sizeof (include_defaults_array));
  2026.       startp = endp = epath;
  2027.       num_dirs = 0;
  2028.       while (1) {
  2029.         /* Handle cases like c:/usr/lib:d:/gcc/lib */
  2030.         if ((*endp == PATH_SEPARATOR
  2031. #if 0 /* Obsolete, now that we use semicolons as the path separator.  */
  2032. #ifdef __MSDOS__
  2033.          && (endp-startp != 1 || !isalpha (*startp))
  2034. #endif
  2035. #endif
  2036.          )
  2037.             || *endp == 0) {
  2038.       strncpy (nstore, startp, endp-startp);
  2039.       if (endp == startp)
  2040.         strcpy (nstore, ".");
  2041.       else
  2042.         nstore[endp-startp] = '\0';
  2043.  
  2044.       include_defaults[num_dirs].fname = savestring (nstore);
  2045.       include_defaults[num_dirs].cplusplus = cplusplus;
  2046.       include_defaults[num_dirs].cxx_aware = 1;
  2047.       num_dirs++;
  2048.       if (*endp == '\0')
  2049.         break;
  2050.       endp = startp = endp + 1;
  2051.     } else
  2052.       endp++;
  2053.       }
  2054.       /* Put the usual defaults back in at the end.  */
  2055.       bcopy ((char *) include_defaults_array,
  2056.          (char *) &include_defaults[num_dirs],
  2057.          sizeof (include_defaults_array));
  2058.     }
  2059.   }
  2060.  
  2061.   append_include_chain (before_system, last_before_system);
  2062.   first_system_include = before_system;
  2063.  
  2064.   /* Unless -fnostdinc,
  2065.      tack on the standard include file dirs to the specified list */
  2066.   if (!no_standard_includes) {
  2067.     struct default_include *p = include_defaults;
  2068.     char *specd_prefix = include_prefix;
  2069.     char *default_prefix = savestring (GCC_INCLUDE_DIR);
  2070.     int default_len = 0;
  2071.     /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  2072.     if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
  2073.       default_len = strlen (default_prefix) - 7;
  2074.       default_prefix[default_len] = 0;
  2075.     }
  2076.     /* Search "translated" versions of GNU directories.
  2077.        These have /usr/local/lib/gcc... replaced by specd_prefix.  */
  2078.     if (specd_prefix != 0 && default_len != 0)
  2079.       for (p = include_defaults; p->fname; p++) {
  2080.     /* Some standard dirs are only for C++.  */
  2081.     if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2082.       /* Does this dir start with the prefix?  */
  2083.       if (!strncmp (p->fname, default_prefix, default_len)) {
  2084.         /* Yes; change prefix and add to search list.  */
  2085.         struct file_name_list *new
  2086.           = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2087.         int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
  2088.         char *str = xmalloc (this_len + 1);
  2089.         strcpy (str, specd_prefix);
  2090.         strcat (str, p->fname + default_len);
  2091.         new->fname = str;
  2092.         new->control_macro = 0;
  2093.         new->c_system_include_path = !p->cxx_aware;
  2094.         new->got_name_map = 0;
  2095.         append_include_chain (new, new);
  2096.         if (first_system_include == 0)
  2097.           first_system_include = new;
  2098.       }
  2099.     }
  2100.       }
  2101.     /* Search ordinary names for GNU include directories.  */
  2102.     for (p = include_defaults; p->fname; p++) {
  2103.       /* Some standard dirs are only for C++.  */
  2104.       if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2105.     struct file_name_list *new
  2106.       = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2107.     new->control_macro = 0;
  2108.     new->c_system_include_path = !p->cxx_aware;
  2109.     new->fname = p->fname;
  2110.     new->got_name_map = 0;
  2111.     append_include_chain (new, new);
  2112.     if (first_system_include == 0)
  2113.       first_system_include = new;
  2114.       }
  2115.     }
  2116.   }
  2117.  
  2118.   /* Tack the after_include chain at the end of the include chain.  */
  2119.   append_include_chain (after_include, last_after_include);
  2120.   if (first_system_include == 0)
  2121.     first_system_include = after_include;
  2122.  
  2123.   /* With -v, print the list of dirs to search.  */
  2124.   if (verbose) {
  2125.     struct file_name_list *p;
  2126.     fprintf (stderr, "#include \"...\" search starts here:\n");
  2127.     for (p = include; p; p = p->next) {
  2128.       if (p == first_bracket_include)
  2129.     fprintf (stderr, "#include <...> search starts here:\n");
  2130.       fprintf (stderr, " %s\n", p->fname);
  2131.     }
  2132.     fprintf (stderr, "End of search list.\n");
  2133.   }
  2134.  
  2135.   /* Scan the -imacros files before the main input.
  2136.      Much like #including them, but with no_output set
  2137.      so that only their macro definitions matter.  */
  2138.  
  2139.   no_output++; no_record_file++;
  2140.   for (i = 1; i < argc; i++)
  2141.     if (pend_files[i]) {
  2142.       int fd = open (pend_files[i], O_RDONLY, 0666);
  2143.       if (fd < 0) {
  2144.     perror_with_name (pend_files[i]);
  2145.     return FATAL_EXIT_CODE;
  2146.       }
  2147.       finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
  2148.     }
  2149.   no_output--; no_record_file--;
  2150.  
  2151.   /* Copy the entire contents of the main input file into
  2152.      the stacked input buffer previously allocated for it.  */
  2153.  
  2154.   /* JF check for stdin */
  2155.   if (in_fname == NULL || *in_fname == 0) {
  2156.     in_fname = "";
  2157.     f = 0;
  2158.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  2159.     goto perror;
  2160.  
  2161.   /* -MG doesn't select the form of output and must be specified with one of
  2162.      -M or -MM.  -MG doesn't make sense with -MD or -MMD since they don't
  2163.      inhibit compilation.  */
  2164.   if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
  2165.     fatal ("-MG must be specified with one of -M or -MM");
  2166.  
  2167.   /* Either of two environment variables can specify output of deps.
  2168.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  2169.      where OUTPUT_FILE is the file to write deps info to
  2170.      and DEPS_TARGET is the target to mention in the deps.  */
  2171.  
  2172.   if (print_deps == 0
  2173.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  2174.       || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
  2175.     char *spec = getenv ("DEPENDENCIES_OUTPUT");
  2176.     char *s;
  2177.     char *output_file;
  2178.  
  2179.     if (spec == 0) {
  2180.       spec = getenv ("SUNPRO_DEPENDENCIES");
  2181.       print_deps = 2;
  2182.     }
  2183.     else
  2184.       print_deps = 1;
  2185.  
  2186.     s = spec;
  2187.     /* Find the space before the DEPS_TARGET, if there is one.  */
  2188.     /* This should use index.  (mrs) */
  2189.     while (*s != 0 && *s != ' ') s++;
  2190.     if (*s != 0) {
  2191.       deps_target = s + 1;
  2192.       output_file = xmalloc (s - spec + 1);
  2193.       bcopy (spec, output_file, s - spec);
  2194.       output_file[s - spec] = 0;
  2195.     }
  2196.     else {
  2197.       deps_target = 0;
  2198.       output_file = spec;
  2199.     }
  2200.       
  2201.     deps_file = output_file;
  2202.     deps_mode = "a";
  2203.   }
  2204.  
  2205.   /* For -M, print the expected object file name
  2206.      as the target of this Make-rule.  */
  2207.   if (print_deps) {
  2208.     deps_allocated_size = 200;
  2209.     deps_buffer = xmalloc (deps_allocated_size);
  2210.     deps_buffer[0] = 0;
  2211.     deps_size = 0;
  2212.     deps_column = 0;
  2213.  
  2214.     if (deps_target) {
  2215.       deps_output (deps_target, ':');
  2216.     } else if (*in_fname == 0) {
  2217.       deps_output ("-", ':');
  2218.     } else {
  2219.       char *p, *q;
  2220.       int len;
  2221.  
  2222.       /* Discard all directory prefixes from filename.  */
  2223. #ifdef FILE_NAME_NONDIRECTORY
  2224.       q = FILE_NAME_NONDIRECTORY (in_fname);
  2225. #else
  2226.       if ((q = rindex (in_fname, '/')) != NULL
  2227. #ifdef DIR_SEPARATOR
  2228.       && (q = rindex (in_fname, DIR_SEPARATOR)) != NULL
  2229. #endif
  2230.       )
  2231.     ++q;
  2232.       else
  2233.     q = in_fname;
  2234. #endif
  2235.       /* Copy remainder to mungable area.  */
  2236.       p = (char *) alloca (strlen(q) + 8);
  2237.       strcpy (p, q);
  2238.  
  2239.       /* Output P, but remove known suffixes.  */
  2240.       len = strlen (p);
  2241.       q = p + len;
  2242.       if (len >= 2
  2243.       && p[len - 2] == '.'
  2244.       && index("cCsSm", p[len - 1]))
  2245.     q = p + (len - 2);
  2246.       else if (len >= 3
  2247.            && p[len - 3] == '.'
  2248.            && p[len - 2] == 'c'
  2249.            && p[len - 1] == 'c')
  2250.     q = p + (len - 3);
  2251.       else if (len >= 4
  2252.            && p[len - 4] == '.'
  2253.            && p[len - 3] == 'c'
  2254.            && p[len - 2] == 'x'
  2255.            && p[len - 1] == 'x')
  2256.     q = p + (len - 4);
  2257.       else if (len >= 4
  2258.            && p[len - 4] == '.'
  2259.            && p[len - 3] == 'c'
  2260.            && p[len - 2] == 'p'
  2261.            && p[len - 1] == 'p')
  2262.     q = p + (len - 4);
  2263.  
  2264.       /* Supply our own suffix.  */
  2265. #ifndef VMS
  2266.       strcpy (q, ".o");
  2267. #else
  2268.       strcpy (q, ".obj");
  2269. #endif
  2270.  
  2271.       deps_output (p, ':');
  2272.       deps_output (in_fname, ' ');
  2273.     }
  2274.   }
  2275.  
  2276.   file_size_and_mode (f, &st_mode, &st_size);
  2277.   fp->nominal_fname = fp->fname = in_fname;
  2278.   fp->lineno = 1;
  2279.   fp->system_header_p = 0;
  2280.   /* JF all this is mine about reading pipes and ttys */
  2281.   if (! S_ISREG (st_mode)) {
  2282.     /* Read input from a file that is not a normal disk file.
  2283.        We cannot preallocate a buffer with the correct size,
  2284.        so we must read in the file a piece at the time and make it bigger.  */
  2285.     int size;
  2286.     int bsize;
  2287.     int cnt;
  2288.  
  2289.     bsize = 2000;
  2290.     size = 0;
  2291.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  2292.     for (;;) {
  2293.       cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
  2294.       if (cnt < 0) goto perror;    /* error! */
  2295.       size += cnt;
  2296.       if (size != bsize) break;    /* End of file */
  2297.       bsize *= 2;
  2298.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  2299.     }
  2300.     fp->length = size;
  2301.   } else {
  2302.     /* Read a file whose size we can determine in advance.
  2303.        For the sake of VMS, st_size is just an upper bound.  */
  2304.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  2305.     fp->length = safe_read (f, (char *) fp->buf, st_size);
  2306.     if (fp->length < 0) goto perror;
  2307.   }
  2308.   fp->bufp = fp->buf;
  2309.   fp->if_stack = if_stack;
  2310.  
  2311.   /* Make sure data ends with a newline.  And put a null after it.  */
  2312.  
  2313.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  2314.       /* Backslash-newline at end is not good enough.  */
  2315.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  2316.     fp->buf[fp->length++] = '\n';
  2317.     missing_newline = 1;
  2318.   }
  2319.   fp->buf[fp->length] = '\0';
  2320.  
  2321.   /* Unless inhibited, convert trigraphs in the input.  */
  2322.  
  2323.   if (!no_trigraphs)
  2324.     trigraph_pcp (fp);
  2325.  
  2326.   /* Now that we know the input file is valid, open the output.  */
  2327.  
  2328.   if (!out_fname || !strcmp (out_fname, ""))
  2329.     out_fname = "stdout";
  2330.   else if (! freopen (out_fname, "w", stdout))
  2331.     pfatal_with_name (out_fname);
  2332.  
  2333.   output_line_directive (fp, &outbuf, 0, same_file);
  2334.  
  2335.   /* Scan the -include files before the main input.  */
  2336.  
  2337.   no_record_file++;
  2338.   for (i = 1; i < argc; i++)
  2339.     if (pend_includes[i]) {
  2340.       int fd = open (pend_includes[i], O_RDONLY, 0666);
  2341.       if (fd < 0) {
  2342.     perror_with_name (pend_includes[i]);
  2343.     return FATAL_EXIT_CODE;
  2344.       }
  2345.       finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR);
  2346.     }
  2347.   no_record_file--;
  2348.  
  2349.   /* Scan the input, processing macros and directives.  */
  2350.  
  2351.   rescan (&outbuf, 0);
  2352.  
  2353.   if (missing_newline)
  2354.     fp->lineno--;
  2355.  
  2356.   if (pedantic && missing_newline)
  2357.     pedwarn ("file does not end in newline");
  2358.  
  2359.   /* Now we have processed the entire input
  2360.      Write whichever kind of output has been requested.  */
  2361.  
  2362.   if (dump_macros == dump_only)
  2363.     dump_all_macros ();
  2364.   else if (! inhibit_output) {
  2365.     write_output ();
  2366.   }
  2367.  
  2368.   if (print_deps) {
  2369.     /* Don't actually write the deps file if compilation has failed.  */
  2370.     if (errors == 0) {
  2371.       if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
  2372.     pfatal_with_name (deps_file);
  2373.       fputs (deps_buffer, deps_stream);
  2374.       putc ('\n', deps_stream);
  2375.       if (deps_file) {
  2376.     if (ferror (deps_stream) || fclose (deps_stream) != 0)
  2377.       fatal ("I/O error on output");
  2378.       }
  2379.     }
  2380.   }
  2381.  
  2382.   if (pcp_outfile && pcp_outfile != stdout
  2383.       && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
  2384.     fatal ("I/O error on `-pcp' output");
  2385.  
  2386.   if (ferror (stdout) || fclose (stdout) != 0)
  2387.     fatal ("I/O error on output");
  2388.  
  2389. /* PhB 25-Jun-95: restore old process priority before exiting */
  2390. #ifdef __amigados__
  2391.   SetTaskPri(amiga_task, amiga_old_priority);
  2392. #endif /* __amigados__ */
  2393.  
  2394.   if (errors)
  2395.     exit (FATAL_EXIT_CODE);
  2396.   exit (SUCCESS_EXIT_CODE);
  2397.  
  2398.  perror:
  2399.   pfatal_with_name (in_fname);
  2400.   return 0;
  2401. }
  2402.  
  2403. /* Given a colon-separated list of file names PATH,
  2404.    add all the names to the search path for include files.  */
  2405.  
  2406. static void
  2407. path_include (path)
  2408.      char *path;
  2409. {
  2410.   char *p;
  2411.  
  2412.   p = path;
  2413.  
  2414.   if (*p)
  2415.     while (1) {
  2416.       char *q = p;
  2417.       char *name;
  2418.       struct file_name_list *dirtmp;
  2419.  
  2420.       /* Find the end of this name.  */
  2421.       while (*q != 0 && *q != PATH_SEPARATOR) q++;
  2422.       if (p == q) {
  2423.     /* An empty name in the path stands for the current directory.  */
  2424.     name = xmalloc (2);
  2425.     name[0] = '.';
  2426.     name[1] = 0;
  2427.       } else {
  2428.     /* Otherwise use the directory that is named.  */
  2429.     name = xmalloc (q - p + 1);
  2430.     bcopy (p, name, q - p);
  2431.     name[q - p] = 0;
  2432.       }
  2433.  
  2434.       dirtmp = (struct file_name_list *)
  2435.     xmalloc (sizeof (struct file_name_list));
  2436.       dirtmp->next = 0;        /* New one goes on the end */
  2437.       dirtmp->control_macro = 0;
  2438.       dirtmp->c_system_include_path = 0;
  2439.       dirtmp->fname = name;
  2440.       dirtmp->got_name_map = 0;
  2441.       append_include_chain (dirtmp, dirtmp);
  2442.  
  2443.       /* Advance past this name.  */
  2444.       p = q;
  2445.       if (*p == 0)
  2446.     break;
  2447.       /* Skip the colon.  */
  2448.       p++;
  2449.     }
  2450. }
  2451.  
  2452. /* Return the address of the first character in S that equals C.
  2453.    S is an array of length N, possibly containing '\0's, and followed by '\0'.
  2454.    Return 0 if there is no such character.  Assume that C itself is not '\0'.
  2455.    If we knew we could use memchr, we could just invoke memchr (S, C, N),
  2456.    but unfortunately memchr isn't autoconfigured yet.  */
  2457.  
  2458. static U_CHAR *
  2459. index0 (s, c, n)
  2460.      U_CHAR *s;
  2461.      int c;
  2462.      size_t n;
  2463. {
  2464.   char *p = (char *) s;
  2465.   for (;;) {
  2466.     char *q = index (p, c);
  2467.     if (q)
  2468.       return (U_CHAR *) q;
  2469.     else {
  2470.       size_t l = strlen (p);
  2471.       if (l == n)
  2472.     return 0;
  2473.       l++;
  2474.       p += l;
  2475.       n -= l;
  2476.     }
  2477.   }
  2478. }
  2479.  
  2480. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  2481.    before main CCCP processing.  Name `pcp' is also in honor of the
  2482.    drugs the trigraph designers must have been on.
  2483.  
  2484.    Using an extra pass through the buffer takes a little extra time,
  2485.    but is infinitely less hairy than trying to handle trigraphs inside
  2486.    strings, etc. everywhere, and also makes sure that trigraphs are
  2487.    only translated in the top level of processing. */
  2488.  
  2489. static void
  2490. trigraph_pcp (buf)
  2491.      FILE_BUF *buf;
  2492. {
  2493.   register U_CHAR c, *fptr, *bptr, *sptr, *lptr;
  2494.   int len;
  2495.  
  2496.   fptr = bptr = sptr = buf->buf;
  2497.   lptr = fptr + buf->length;
  2498.   while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
  2499.     if (*++sptr != '?')
  2500.       continue;
  2501.     switch (*++sptr) {
  2502.       case '=':
  2503.       c = '#';
  2504.       break;
  2505.     case '(':
  2506.       c = '[';
  2507.       break;
  2508.     case '/':
  2509.       c = '\\';
  2510.       break;
  2511.     case ')':
  2512.       c = ']';
  2513.       break;
  2514.     case '\'':
  2515.       c = '^';
  2516.       break;
  2517.     case '<':
  2518.       c = '{';
  2519.       break;
  2520.     case '!':
  2521.       c = '|';
  2522.       break;
  2523.     case '>':
  2524.       c = '}';
  2525.       break;
  2526.     case '-':
  2527.       c  = '~';
  2528.       break;
  2529.     case '?':
  2530.       sptr--;
  2531.       continue;
  2532.     default:
  2533.       continue;
  2534.     }
  2535.     len = sptr - fptr - 2;
  2536.  
  2537.     /* BSD doc says bcopy () works right for overlapping strings.  In ANSI
  2538.        C, this will be memmove (). */
  2539.     if (bptr != fptr && len > 0)
  2540.       bcopy ((char *) fptr, (char *) bptr, len);
  2541.  
  2542.     bptr += len;
  2543.     *bptr++ = c;
  2544.     fptr = ++sptr;
  2545.   }
  2546.   len = buf->length - (fptr - buf->buf);
  2547.   if (bptr != fptr && len > 0)
  2548.     bcopy ((char *) fptr, (char *) bptr, len);
  2549.   buf->length -= fptr - bptr;
  2550.   buf->buf[buf->length] = '\0';
  2551.   if (warn_trigraphs && fptr != bptr)
  2552.     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
  2553. }
  2554.  
  2555. /* Move all backslash-newline pairs out of embarrassing places.
  2556.    Exchange all such pairs following BP
  2557.    with any potentially-embarrassing characters that follow them.
  2558.    Potentially-embarrassing characters are / and *
  2559.    (because a backslash-newline inside a comment delimiter
  2560.    would cause it not to be recognized).  */
  2561.  
  2562. static void
  2563. newline_fix (bp)
  2564.      U_CHAR *bp;
  2565. {
  2566.   register U_CHAR *p = bp;
  2567.  
  2568.   /* First count the backslash-newline pairs here.  */
  2569.  
  2570.   while (p[0] == '\\' && p[1] == '\n')
  2571.     p += 2;
  2572.  
  2573.   /* What follows the backslash-newlines is not embarrassing.  */
  2574.  
  2575.   if (*p != '/' && *p != '*')
  2576.     return;
  2577.  
  2578.   /* Copy all potentially embarrassing characters
  2579.      that follow the backslash-newline pairs
  2580.      down to where the pairs originally started.  */
  2581.  
  2582.   while (*p == '*' || *p == '/')
  2583.     *bp++ = *p++;
  2584.  
  2585.   /* Now write the same number of pairs after the embarrassing chars.  */
  2586.   while (bp < p) {
  2587.     *bp++ = '\\';
  2588.     *bp++ = '\n';
  2589.   }
  2590. }
  2591.  
  2592. /* Like newline_fix but for use within a directive-name.
  2593.    Move any backslash-newlines up past any following symbol constituents.  */
  2594.  
  2595. static void
  2596. name_newline_fix (bp)
  2597.      U_CHAR *bp;
  2598. {
  2599.   register U_CHAR *p = bp;
  2600.  
  2601.   /* First count the backslash-newline pairs here.  */
  2602.   while (p[0] == '\\' && p[1] == '\n')
  2603.     p += 2;
  2604.  
  2605.   /* What follows the backslash-newlines is not embarrassing.  */
  2606.  
  2607.   if (!is_idchar[*p])
  2608.     return;
  2609.  
  2610.   /* Copy all potentially embarrassing characters
  2611.      that follow the backslash-newline pairs
  2612.      down to where the pairs originally started.  */
  2613.  
  2614.   while (is_idchar[*p])
  2615.     *bp++ = *p++;
  2616.  
  2617.   /* Now write the same number of pairs after the embarrassing chars.  */
  2618.   while (bp < p) {
  2619.     *bp++ = '\\';
  2620.     *bp++ = '\n';
  2621.   }
  2622. }
  2623.  
  2624. /* Look for lint commands in comments.
  2625.  
  2626.    When we come in here, ibp points into a comment.  Limit is as one expects.
  2627.    scan within the comment -- it should start, after lwsp, with a lint command.
  2628.    If so that command is returned as a (constant) string.
  2629.  
  2630.    Upon return, any arg will be pointed to with argstart and will be
  2631.    arglen long.  Note that we don't parse that arg since it will just
  2632.    be printed out again.
  2633. */
  2634.  
  2635. static char *
  2636. get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
  2637.      register U_CHAR *ibp;
  2638.      register U_CHAR *limit;
  2639.      U_CHAR **argstart;        /* point to command arg */
  2640.      int *arglen, *cmdlen;    /* how long they are */
  2641. {
  2642.   long linsize;
  2643.   register U_CHAR *numptr;    /* temp for arg parsing */
  2644.  
  2645.   *arglen = 0;
  2646.  
  2647.   SKIP_WHITE_SPACE (ibp);
  2648.  
  2649.   if (ibp >= limit) return NULL;
  2650.  
  2651.   linsize = limit - ibp;
  2652.   
  2653.   /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
  2654.   if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) {
  2655.     *cmdlen = 10;
  2656.     return "NOTREACHED";
  2657.   }
  2658.   if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
  2659.     *cmdlen = 8;
  2660.     return "ARGSUSED";
  2661.   }
  2662.   if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
  2663.     *cmdlen = 11;
  2664.     return "LINTLIBRARY";
  2665.   }
  2666.   if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
  2667.     *cmdlen = 7;
  2668.     ibp += 7; linsize -= 7;
  2669.     if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
  2670.  
  2671.     /* OK, read a number */
  2672.     for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
  2673.      numptr++);
  2674.     *arglen = numptr - *argstart;
  2675.     return "VARARGS";
  2676.   }
  2677.   return NULL;
  2678. }
  2679.  
  2680. /*
  2681.  * The main loop of the program.
  2682.  *
  2683.  * Read characters from the input stack, transferring them to the
  2684.  * output buffer OP.
  2685.  *
  2686.  * Macros are expanded and push levels on the input stack.
  2687.  * At the end of such a level it is popped off and we keep reading.
  2688.  * At the end of any other kind of level, we return.
  2689.  * #-directives are handled, except within macros.
  2690.  *
  2691.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  2692.  * and insert them when appropriate.  This is set while scanning macro
  2693.  * arguments before substitution.  It is zero when scanning for final output.
  2694.  *   There are three types of Newline markers:
  2695.  *   * Newline -  follows a macro name that was not expanded
  2696.  *     because it appeared inside an expansion of the same macro.
  2697.  *     This marker prevents future expansion of that identifier.
  2698.  *     When the input is rescanned into the final output, these are deleted.
  2699.  *     These are also deleted by ## concatenation.
  2700.  *   * Newline Space (or Newline and any other whitespace character)
  2701.  *     stands for a place that tokens must be separated or whitespace
  2702.  *     is otherwise desirable, but where the ANSI standard specifies there
  2703.  *     is no whitespace.  This marker turns into a Space (or whichever other
  2704.  *     whitespace char appears in the marker) in the final output,
  2705.  *     but it turns into nothing in an argument that is stringified with #.
  2706.  *     Such stringified arguments are the only place where the ANSI standard
  2707.  *     specifies with precision that whitespace may not appear.
  2708.  *
  2709.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  2710.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  2711.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  2712.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  2713.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  2714.  */
  2715.  
  2716. static void
  2717. rescan (op, output_marks)
  2718.      FILE_BUF *op;
  2719.      int output_marks;
  2720. {
  2721.   /* Character being scanned in main loop.  */
  2722.   register U_CHAR c;
  2723.  
  2724.   /* Length of pending accumulated identifier.  */
  2725.   register int ident_length = 0;
  2726.  
  2727.   /* Hash code of pending accumulated identifier.  */
  2728.   register int hash = 0;
  2729.  
  2730.   /* Current input level (&instack[indepth]).  */
  2731.   FILE_BUF *ip;
  2732.  
  2733.   /* Pointer for scanning input.  */
  2734.   register U_CHAR *ibp;
  2735.  
  2736.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  2737.   register U_CHAR *limit;
  2738.  
  2739.   /* Pointer for storing output.  */
  2740.   register U_CHAR *obp;
  2741.  
  2742.   /* REDO_CHAR is nonzero if we are processing an identifier
  2743.      after backing up over the terminating character.
  2744.      Sometimes we process an identifier without backing up over
  2745.      the terminating character, if the terminating character
  2746.      is not special.  Backing up is done so that the terminating character
  2747.      will be dispatched on again once the identifier is dealt with.  */
  2748.   int redo_char = 0;
  2749.  
  2750.   /* 1 if within an identifier inside of which a concatenation
  2751.      marker (Newline -) has been seen.  */
  2752.   int concatenated = 0;
  2753.  
  2754.   /* While scanning a comment or a string constant,
  2755.      this records the line it started on, for error messages.  */
  2756.   int start_line;
  2757.  
  2758.   /* Record position of last `real' newline.  */
  2759.   U_CHAR *beg_of_line;
  2760.  
  2761. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  2762.  
  2763. #define POPMACRO \
  2764. do { ip->macro->type = T_MACRO;        \
  2765.      if (ip->free_ptr) free (ip->free_ptr);    \
  2766.      --indepth; } while (0)
  2767.  
  2768. /* Reload `rescan's local variables that describe the current
  2769.    level of the input stack.  */
  2770.  
  2771. #define RECACHE  \
  2772. do { ip = &instack[indepth];        \
  2773.      ibp = ip->bufp;            \
  2774.      limit = ip->buf + ip->length;    \
  2775.      op->bufp = obp;            \
  2776.      check_expand (op, limit - ibp);    \
  2777.      beg_of_line = 0;            \
  2778.      obp = op->bufp; } while (0)
  2779.  
  2780.   if (no_output && instack[indepth].fname != 0)
  2781.     skip_if_group (&instack[indepth], 1, NULL);
  2782.  
  2783.   obp = op->bufp;
  2784.   RECACHE;
  2785.  
  2786.   beg_of_line = ibp;
  2787.  
  2788.   /* Our caller must always put a null after the end of
  2789.      the input at each input stack level.  */
  2790.   if (*limit != 0)
  2791.     abort ();
  2792.  
  2793.   while (1) {
  2794.     c = *ibp++;
  2795.     *obp++ = c;
  2796.  
  2797.     switch (c) {
  2798.     case '\\':
  2799.       if (*ibp == '\n' && !ip->macro) {
  2800.     /* At the top level, always merge lines ending with backslash-newline,
  2801.        even in middle of identifier.  But do not merge lines in a macro,
  2802.        since backslash might be followed by a newline-space marker.  */
  2803.     ++ibp;
  2804.     ++ip->lineno;
  2805.     --obp;        /* remove backslash from obuf */
  2806.     break;
  2807.       }
  2808.       /* If ANSI, backslash is just another character outside a string.  */
  2809.       if (!traditional)
  2810.     goto randomchar;
  2811.       /* Otherwise, backslash suppresses specialness of following char,
  2812.      so copy it here to prevent the switch from seeing it.
  2813.      But first get any pending identifier processed.  */
  2814.       if (ident_length > 0)
  2815.     goto specialchar;
  2816.       if (ibp < limit)
  2817.     *obp++ = *ibp++;
  2818.       break;
  2819.  
  2820.     case '%':
  2821.       if (ident_length || ip->macro || traditional)
  2822.     goto randomchar;
  2823.       while (*ibp == '\\' && ibp[1] == '\n') {
  2824.     ibp += 2;
  2825.     ++ip->lineno;
  2826.       }
  2827.       if (*ibp != ':')
  2828.     break;
  2829.       /* Treat this %: digraph as if it were #.  */
  2830.       /* Fall through.  */
  2831.  
  2832.     case '#':
  2833.       if (assertions_flag) {
  2834.     /* Copy #foo (bar lose) without macro expansion.  */
  2835.     obp[-1] = '#';    /* In case it was '%'. */
  2836.     SKIP_WHITE_SPACE (ibp);
  2837.     while (is_idchar[*ibp])
  2838.       *obp++ = *ibp++;
  2839.     SKIP_WHITE_SPACE (ibp);
  2840.     if (*ibp == '(') {
  2841.       ip->bufp = ibp;
  2842.       skip_paren_group (ip);
  2843.       bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
  2844.       obp += ip->bufp - ibp;
  2845.       ibp = ip->bufp;
  2846.     }
  2847.       }
  2848.  
  2849.       /* If this is expanding a macro definition, don't recognize
  2850.      preprocessing directives.  */
  2851.       if (ip->macro != 0)
  2852.     goto randomchar;
  2853.       /* If this is expand_into_temp_buffer,
  2854.      don't recognize them either.  Warn about them
  2855.      only after an actual newline at this level,
  2856.      not at the beginning of the input level.  */
  2857.       if (! ip->fname) {
  2858.     if (ip->buf != beg_of_line)
  2859.       warning ("preprocessing directive not recognized within macro arg");
  2860.     goto randomchar;
  2861.       }
  2862.       if (ident_length)
  2863.     goto specialchar;
  2864.  
  2865.       
  2866.       /* # keyword: a # must be first nonblank char on the line */
  2867.       if (beg_of_line == 0)
  2868.     goto randomchar;
  2869.       {
  2870.     U_CHAR *bp;
  2871.  
  2872.     /* Scan from start of line, skipping whitespace, comments
  2873.        and backslash-newlines, and see if we reach this #.
  2874.        If not, this # is not special.  */
  2875.     bp = beg_of_line;
  2876.     /* If -traditional, require # to be at beginning of line.  */
  2877.     if (!traditional) {
  2878.       while (1) {
  2879.         if (is_hor_space[*bp])
  2880.           bp++;
  2881.         else if (*bp == '\\' && bp[1] == '\n')
  2882.           bp += 2;
  2883.         else if (*bp == '/' && bp[1] == '*') {
  2884.           bp += 2;
  2885.           while (!(*bp == '*' && bp[1] == '/'))
  2886.         bp++;
  2887.           bp += 2;
  2888.         }
  2889.         /* There is no point in trying to deal with C++ // comments here,
  2890.            because if there is one, then this # must be part of the
  2891.            comment and we would never reach here.  */
  2892.         else break;
  2893.       }
  2894.       if (c == '%') {
  2895.         if (bp[0] != '%')
  2896.           break;
  2897.         while (bp[1] == '\\' && bp[2] == '\n')
  2898.           bp += 2;
  2899.         if (bp + 1 != ibp)
  2900.           break;
  2901.         /* %: appears at start of line; skip past the ':' too.  */
  2902.         bp++;
  2903.         ibp++;
  2904.       }
  2905.     }
  2906.     if (bp + 1 != ibp)
  2907.       goto randomchar;
  2908.       }
  2909.  
  2910.       /* This # can start a directive.  */
  2911.  
  2912.       --obp;        /* Don't copy the '#' */
  2913.  
  2914.       ip->bufp = ibp;
  2915.       op->bufp = obp;
  2916.       if (! handle_directive (ip, op)) {
  2917. #ifdef USE_C_ALLOCA
  2918.     alloca (0);
  2919. #endif
  2920.     /* Not a known directive: treat it as ordinary text.
  2921.        IP, OP, IBP, etc. have not been changed.  */
  2922.     if (no_output && instack[indepth].fname) {
  2923.       /* If not generating expanded output,
  2924.          what we do with ordinary text is skip it.
  2925.          Discard everything until next # directive.  */
  2926.       skip_if_group (&instack[indepth], 1, 0);
  2927.       RECACHE;
  2928.       beg_of_line = ibp;
  2929.       break;
  2930.     }
  2931.     *obp++ = '#';    /* Copy # (even if it was originally %:).  */
  2932.     /* Don't expand an identifier that could be a macro directive.
  2933.        (Section 3.8.3 of the ANSI C standard)            */
  2934.     SKIP_WHITE_SPACE (ibp);
  2935.     if (is_idstart[*ibp])
  2936.       {
  2937.         *obp++ = *ibp++;
  2938.         while (is_idchar[*ibp])
  2939.           *obp++ = *ibp++;
  2940.       }
  2941.     goto randomchar;
  2942.       }
  2943. #ifdef USE_C_ALLOCA
  2944.       alloca (0);
  2945. #endif
  2946.       /* A # directive has been successfully processed.  */
  2947.       /* If not generating expanded output, ignore everything until
  2948.      next # directive.  */
  2949.       if (no_output && instack[indepth].fname)
  2950.     skip_if_group (&instack[indepth], 1, 0);
  2951.       obp = op->bufp;
  2952.       RECACHE;
  2953.       beg_of_line = ibp;
  2954.       break;
  2955.  
  2956.     case '\"':            /* skip quoted string */
  2957.     case '\'':
  2958.       /* A single quoted string is treated like a double -- some
  2959.      programs (e.g., troff) are perverse this way */
  2960.  
  2961.       if (ident_length)
  2962.     goto specialchar;
  2963.  
  2964.       start_line = ip->lineno;
  2965.  
  2966.       /* Skip ahead to a matching quote.  */
  2967.  
  2968.       while (1) {
  2969.     if (ibp >= limit) {
  2970.       if (ip->macro != 0) {
  2971.         /* try harder: this string crosses a macro expansion boundary.
  2972.            This can happen naturally if -traditional.
  2973.            Otherwise, only -D can make a macro with an unmatched quote.  */
  2974.         POPMACRO;
  2975.         RECACHE;
  2976.         continue;
  2977.       }
  2978.       if (!traditional) {
  2979.         error_with_line (line_for_error (start_line),
  2980.                  "unterminated string or character constant");
  2981.         error_with_line (multiline_string_line,
  2982.                  "possible real start of unterminated constant");
  2983.         multiline_string_line = 0;
  2984.       }
  2985.       break;
  2986.     }
  2987.     *obp++ = *ibp;
  2988.     switch (*ibp++) {
  2989.     case '\n':
  2990.       ++ip->lineno;
  2991.       ++op->lineno;
  2992.       /* Traditionally, end of line ends a string constant with no error.
  2993.          So exit the loop and record the new line.  */
  2994.       if (traditional) {
  2995.         beg_of_line = ibp;
  2996.         goto while2end;
  2997.       }
  2998.       if (c == '\'') {
  2999.         error_with_line (line_for_error (start_line),
  3000.                  "unterminated character constant");
  3001.         goto while2end;
  3002.       }
  3003.       if (pedantic && multiline_string_line == 0) {
  3004.         pedwarn_with_line (line_for_error (start_line),
  3005.                    "string constant runs past end of line");
  3006.       }
  3007.       if (multiline_string_line == 0)
  3008.         multiline_string_line = ip->lineno - 1;
  3009.       break;
  3010.  
  3011.     case '\\':
  3012.       if (ibp >= limit)
  3013.         break;
  3014.       if (*ibp == '\n') {
  3015.         /* Backslash newline is replaced by nothing at all,
  3016.            but keep the line counts correct.  */
  3017.         --obp;
  3018.         ++ibp;
  3019.         ++ip->lineno;
  3020.       } else {
  3021.         /* ANSI stupidly requires that in \\ the second \
  3022.            is *not* prevented from combining with a newline.  */
  3023.         while (*ibp == '\\' && ibp[1] == '\n') {
  3024.           ibp += 2;
  3025.           ++ip->lineno;
  3026.         }
  3027.         *obp++ = *ibp++;
  3028.       }
  3029.       break;
  3030.  
  3031.     case '\"':
  3032.     case '\'':
  3033.       if (ibp[-1] == c)
  3034.         goto while2end;
  3035.       break;
  3036.     }
  3037.       }
  3038.     while2end:
  3039.       break;
  3040.  
  3041.     case '/':
  3042.       if (*ibp == '\\' && ibp[1] == '\n')
  3043.     newline_fix (ibp);
  3044.  
  3045.       if (*ibp != '*'
  3046.       && !(cplusplus_comments && *ibp == '/'))
  3047.     goto randomchar;
  3048.       if (ip->macro != 0)
  3049.     goto randomchar;
  3050.       if (ident_length)
  3051.     goto specialchar;
  3052.  
  3053.       if (*ibp == '/') {
  3054.     /* C++ style comment... */
  3055.     start_line = ip->lineno;
  3056.  
  3057.     --ibp;            /* Back over the slash */
  3058.     --obp;
  3059.  
  3060.     /* Comments are equivalent to spaces. */
  3061.     if (! put_out_comments)
  3062.       *obp++ = ' ';
  3063.     else {
  3064.       /* must fake up a comment here */
  3065.       *obp++ = '/';
  3066.       *obp++ = '/';
  3067.     }
  3068.     {
  3069.       U_CHAR *before_bp = ibp+2;
  3070.  
  3071.       while (ibp < limit) {
  3072.         if (ibp[-1] != '\\' && *ibp == '\n') {
  3073.           if (put_out_comments) {
  3074.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  3075.         obp += ibp - before_bp;
  3076.           }
  3077.           break;
  3078.         } else {
  3079.           if (*ibp == '\n') {
  3080.         ++ip->lineno;
  3081.         /* Copy the newline into the output buffer, in order to
  3082.            avoid the pain of a #line every time a multiline comment
  3083.            is seen.  */
  3084.         if (!put_out_comments)
  3085.           *obp++ = '\n';
  3086.         ++op->lineno;
  3087.           }
  3088.           ibp++;
  3089.         }
  3090.       }
  3091.       break;
  3092.     }
  3093.       }
  3094.  
  3095.       /* Ordinary C comment.  Skip it, optionally copying it to output.  */
  3096.  
  3097.       start_line = ip->lineno;
  3098.  
  3099.       ++ibp;            /* Skip the star. */
  3100.  
  3101.       /* If this cpp is for lint, we peek inside the comments: */
  3102.       if (for_lint) {
  3103.     U_CHAR *argbp;
  3104.     int cmdlen, arglen;
  3105.     char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
  3106.  
  3107.     if (lintcmd != NULL) {
  3108.       op->bufp = obp;
  3109.       check_expand (op, cmdlen + arglen + 14);
  3110.       obp = op->bufp;
  3111.       /* I believe it is always safe to emit this newline: */
  3112.       obp[-1] = '\n';
  3113.       bcopy ("#pragma lint ", (char *) obp, 13);
  3114.       obp += 13;
  3115.       bcopy (lintcmd, (char *) obp, cmdlen);
  3116.       obp += cmdlen;
  3117.  
  3118.       if (arglen != 0) {
  3119.         *(obp++) = ' ';
  3120.         bcopy (argbp, (char *) obp, arglen);
  3121.         obp += arglen;
  3122.       }
  3123.  
  3124.       /* OK, now bring us back to the state we were in before we entered
  3125.          this branch.  We need #line because the #pragma's newline always
  3126.          messes up the line count.  */
  3127.       op->bufp = obp;
  3128.       output_line_directive (ip, op, 0, same_file);
  3129.       check_expand (op, limit - ibp + 2);
  3130.       obp = op->bufp;
  3131.       *(obp++) = '/';
  3132.     }
  3133.       }
  3134.  
  3135.       /* Comments are equivalent to spaces.
  3136.      Note that we already output the slash; we might not want it.
  3137.      For -traditional, a comment is equivalent to nothing.  */
  3138.       if (! put_out_comments) {
  3139.     if (traditional)
  3140.       obp--;
  3141.     else
  3142.       obp[-1] = ' ';
  3143.       }
  3144.       else
  3145.     *obp++ = '*';
  3146.  
  3147.       {
  3148.     U_CHAR *before_bp = ibp;
  3149.  
  3150.     while (ibp < limit) {
  3151.       switch (*ibp++) {
  3152.       case '/':
  3153.         if (warn_comments && *ibp == '*')
  3154.           warning ("`/*' within comment");
  3155.         break;
  3156.       case '*':
  3157.         if (*ibp == '\\' && ibp[1] == '\n')
  3158.           newline_fix (ibp);
  3159.         if (ibp >= limit || *ibp == '/')
  3160.           goto comment_end;
  3161.         break;
  3162.       case '\n':
  3163.         ++ip->lineno;
  3164.         /* Copy the newline into the output buffer, in order to
  3165.            avoid the pain of a #line every time a multiline comment
  3166.            is seen.  */
  3167.         if (!put_out_comments)
  3168.           *obp++ = '\n';
  3169.         ++op->lineno;
  3170.       }
  3171.     }
  3172.       comment_end:
  3173.  
  3174.     if (ibp >= limit)
  3175.       error_with_line (line_for_error (start_line),
  3176.                "unterminated comment");
  3177.     else {
  3178.       ibp++;
  3179.       if (put_out_comments) {
  3180.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  3181.         obp += ibp - before_bp;
  3182.       }
  3183.     }
  3184.       }
  3185.       break;
  3186.  
  3187.     case '$':
  3188.       if (!dollars_in_ident)
  3189.     goto randomchar;
  3190.       goto letter;
  3191.  
  3192.     case '0': case '1': case '2': case '3': case '4':
  3193.     case '5': case '6': case '7': case '8': case '9':
  3194.       /* If digit is not part of identifier, it starts a number,
  3195.      which means that following letters are not an identifier.
  3196.      "0x5" does not refer to an identifier "x5".
  3197.      So copy all alphanumerics that follow without accumulating
  3198.      as an identifier.  Periods also, for sake of "3.e7".  */
  3199.  
  3200.       if (ident_length == 0) {
  3201.     for (;;) {
  3202.       while (ibp[0] == '\\' && ibp[1] == '\n') {
  3203.         ++ip->lineno;
  3204.         ibp += 2;
  3205.       }
  3206.       c = *ibp++;
  3207.       if (!is_idchar[c] && c != '.') {
  3208.         --ibp;
  3209.         break;
  3210.       }
  3211.       *obp++ = c;
  3212.       /* A sign can be part of a preprocessing number
  3213.          if it follows an e.  */
  3214.       if (c == 'e' || c == 'E') {
  3215.         while (ibp[0] == '\\' && ibp[1] == '\n') {
  3216.           ++ip->lineno;
  3217.           ibp += 2;
  3218.         }
  3219.         if (*ibp == '+' || *ibp == '-') {
  3220.           *obp++ = *ibp++;
  3221.           /* But traditional C does not let the token go past the sign.  */
  3222.           if (traditional)
  3223.         break;
  3224.         }
  3225.       }
  3226.     }
  3227.     break;
  3228.       }
  3229.       /* fall through */
  3230.  
  3231.     case '_':
  3232.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  3233.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  3234.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  3235.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  3236.     case 'y': case 'z':
  3237.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  3238.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  3239.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  3240.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  3241.     case 'Y': case 'Z':
  3242.     letter:
  3243.       ident_length++;
  3244.       /* Compute step of hash function, to avoid a proc call on every token */
  3245.       hash = HASHSTEP (hash, c);
  3246.       break;
  3247.  
  3248.     case '\n':
  3249.       if (ip->fname == 0 && *ibp == '-') {
  3250.     /* Newline - inhibits expansion of preceding token.
  3251.        If expanding a macro arg, we keep the newline -.
  3252.        In final output, it is deleted.
  3253.        We recognize Newline - in macro bodies and macro args.  */
  3254.     if (! concatenated) {
  3255.       ident_length = 0;
  3256.       hash = 0;
  3257.     }
  3258.     ibp++;
  3259.     if (!output_marks) {
  3260.       obp--;
  3261.     } else {
  3262.       /* If expanding a macro arg, keep the newline -.  */
  3263.       *obp++ = '-';
  3264.     }
  3265.     break;
  3266.       }
  3267.  
  3268.       /* If reprocessing a macro expansion, newline is a special marker.  */
  3269.       else if (ip->macro != 0) {
  3270.     /* Newline White is a "funny space" to separate tokens that are
  3271.        supposed to be separate but without space between.
  3272.        Here White means any whitespace character.
  3273.        Newline - marks a recursive macro use that is not
  3274.        supposed to be expandable.  */
  3275.  
  3276.     if (is_space[*ibp]) {
  3277.       /* Newline Space does not prevent expansion of preceding token
  3278.          so expand the preceding token and then come back.  */
  3279.       if (ident_length > 0)
  3280.         goto specialchar;
  3281.  
  3282.       /* If generating final output, newline space makes a space.  */
  3283.       if (!output_marks) {
  3284.         obp[-1] = *ibp++;
  3285.         /* And Newline Newline makes a newline, so count it.  */
  3286.         if (obp[-1] == '\n')
  3287.           op->lineno++;
  3288.       } else {
  3289.         /* If expanding a macro arg, keep the newline space.
  3290.            If the arg gets stringified, newline space makes nothing.  */
  3291.         *obp++ = *ibp++;
  3292.       }
  3293.     } else abort ();    /* Newline followed by something random?  */
  3294.     break;
  3295.       }
  3296.  
  3297.       /* If there is a pending identifier, handle it and come back here.  */
  3298.       if (ident_length > 0)
  3299.     goto specialchar;
  3300.  
  3301.       beg_of_line = ibp;
  3302.  
  3303.       /* Update the line counts and output a #line if necessary.  */
  3304.       ++ip->lineno;
  3305.       ++op->lineno;
  3306.       if (ip->lineno != op->lineno) {
  3307.     op->bufp = obp;
  3308.     output_line_directive (ip, op, 1, same_file);
  3309.     check_expand (op, limit - ibp);
  3310.     obp = op->bufp;
  3311.       }
  3312.       break;
  3313.  
  3314.       /* Come here either after (1) a null character that is part of the input
  3315.      or (2) at the end of the input, because there is a null there.  */
  3316.     case 0:
  3317.       if (ibp <= limit)
  3318.     /* Our input really contains a null character.  */
  3319.     goto randomchar;
  3320.  
  3321.       /* At end of a macro-expansion level, pop it and read next level.  */
  3322.       if (ip->macro != 0) {
  3323.     obp--;
  3324.     ibp--;
  3325.     /* If traditional, and we have an identifier that ends here,
  3326.        process it now, so we get the right error for recursion.  */
  3327.     if (traditional && ident_length
  3328.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  3329.       redo_char = 1;
  3330.       goto randomchar;
  3331.     }
  3332.     POPMACRO;
  3333.     RECACHE;
  3334.     break;
  3335.       }
  3336.  
  3337.       /* If we don't have a pending identifier,
  3338.      return at end of input.  */
  3339.       if (ident_length == 0) {
  3340.     obp--;
  3341.     ibp--;
  3342.     op->bufp = obp;
  3343.     ip->bufp = ibp;
  3344.     goto ending;
  3345.       }
  3346.  
  3347.       /* If we do have a pending identifier, just consider this null
  3348.      a special character and arrange to dispatch on it again.
  3349.      The second time, IDENT_LENGTH will be zero so we will return.  */
  3350.  
  3351.       /* Fall through */
  3352.  
  3353. specialchar:
  3354.  
  3355.       /* Handle the case of a character such as /, ', " or null
  3356.      seen following an identifier.  Back over it so that
  3357.      after the identifier is processed the special char
  3358.      will be dispatched on again.  */
  3359.  
  3360.       ibp--;
  3361.       obp--;
  3362.       redo_char = 1;
  3363.  
  3364.     default:
  3365.  
  3366. randomchar:
  3367.  
  3368.       if (ident_length > 0) {
  3369.     register HASHNODE *hp;
  3370.  
  3371.     /* We have just seen an identifier end.  If it's a macro, expand it.
  3372.  
  3373.        IDENT_LENGTH is the length of the identifier
  3374.        and HASH is its hash code.
  3375.  
  3376.        The identifier has already been copied to the output,
  3377.        so if it is a macro we must remove it.
  3378.  
  3379.        If REDO_CHAR is 0, the char that terminated the identifier
  3380.        has been skipped in the output and the input.
  3381.        OBP-IDENT_LENGTH-1 points to the identifier.
  3382.        If the identifier is a macro, we must back over the terminator.
  3383.  
  3384.        If REDO_CHAR is 1, the terminating char has already been
  3385.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  3386.  
  3387.     if (!pcp_outfile || pcp_inside_if) {
  3388.       for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  3389.            hp = hp->next) {
  3390.         
  3391.         if (hp->length == ident_length) {
  3392.           int obufp_before_macroname;
  3393.           int op_lineno_before_macroname;
  3394.           register int i = ident_length;
  3395.           register U_CHAR *p = hp->name;
  3396.           register U_CHAR *q = obp - i;
  3397.           int disabled;
  3398.           
  3399.           if (! redo_char)
  3400.         q--;
  3401.           
  3402.           do {        /* All this to avoid a strncmp () */
  3403.         if (*p++ != *q++)
  3404.           goto hashcollision;
  3405.           } while (--i);
  3406.           
  3407.           /* We found a use of a macro name.
  3408.          see if the context shows it is a macro call.  */
  3409.           
  3410.           /* Back up over terminating character if not already done.  */
  3411.           if (! redo_char) {
  3412.         ibp--;
  3413.         obp--;
  3414.           }
  3415.           
  3416.           /* Save this as a displacement from the beginning of the output
  3417.          buffer.  We can not save this as a position in the output
  3418.          buffer, because it may get realloc'ed by RECACHE.  */
  3419.           obufp_before_macroname = (obp - op->buf) - ident_length;
  3420.           op_lineno_before_macroname = op->lineno;
  3421.           
  3422.           if (hp->type == T_PCSTRING) {
  3423.         pcstring_used (hp); /* Mark the definition of this key
  3424.                        as needed, ensuring that it
  3425.                        will be output.  */
  3426.         break;        /* Exit loop, since the key cannot have a
  3427.                    definition any longer.  */
  3428.           }
  3429.  
  3430.           /* Record whether the macro is disabled.  */
  3431.           disabled = hp->type == T_DISABLED;
  3432.           
  3433.           /* This looks like a macro ref, but if the macro was disabled,
  3434.          just copy its name and put in a marker if requested.  */
  3435.           
  3436.           if (disabled) {
  3437. #if 0
  3438.         /* This error check caught useful cases such as
  3439.            #define foo(x,y) bar (x (y,0), y)
  3440.            foo (foo, baz)  */
  3441.         if (traditional)
  3442.           error ("recursive use of macro `%s'", hp->name);
  3443. #endif
  3444.         
  3445.         if (output_marks) {
  3446.           check_expand (op, limit - ibp + 2);
  3447.           *obp++ = '\n';
  3448.           *obp++ = '-';
  3449.         }
  3450.         break;
  3451.           }
  3452.           
  3453.           /* If macro wants an arglist, verify that a '(' follows.
  3454.          first skip all whitespace, copying it to the output
  3455.          after the macro name.  Then, if there is no '(',
  3456.          decide this is not a macro call and leave things that way.  */
  3457.           if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  3458.           && hp->value.defn->nargs >= 0)
  3459.         {
  3460.           U_CHAR *old_ibp = ibp;
  3461.           U_CHAR *old_obp = obp;
  3462.           int old_iln = ip->lineno;
  3463.           int old_oln = op->lineno;
  3464.           
  3465.           while (1) {
  3466.             /* Scan forward over whitespace, copying it to the output.  */
  3467.             if (ibp == limit && ip->macro != 0) {
  3468.               POPMACRO;
  3469.               RECACHE;
  3470.               old_ibp = ibp;
  3471.               old_obp = obp;
  3472.               old_iln = ip->lineno;
  3473.               old_oln = op->lineno;
  3474.             }
  3475.             /* A comment: copy it unchanged or discard it.  */
  3476.             else if (*ibp == '/' && ibp[1] == '*') {
  3477.               if (put_out_comments) {
  3478.             *obp++ = '/';
  3479.             *obp++ = '*';
  3480.               } else if (! traditional) {
  3481.             *obp++ = ' ';
  3482.               }
  3483.               ibp += 2;
  3484.               while (ibp + 1 != limit
  3485.                  && !(ibp[0] == '*' && ibp[1] == '/')) {
  3486.             /* We need not worry about newline-marks,
  3487.                since they are never found in comments.  */
  3488.             if (*ibp == '\n') {
  3489.               /* Newline in a file.  Count it.  */
  3490.               ++ip->lineno;
  3491.               ++op->lineno;
  3492.             }
  3493.             if (put_out_comments)
  3494.               *obp++ = *ibp++;
  3495.             else
  3496.               ibp++;
  3497.               }
  3498.               ibp += 2;
  3499.               if (put_out_comments) {
  3500.             *obp++ = '*';
  3501.             *obp++ = '/';
  3502.               }
  3503.             }
  3504.             else if (is_space[*ibp]) {
  3505.               *obp++ = *ibp++;
  3506.               if (ibp[-1] == '\n') {
  3507.             if (ip->macro == 0) {
  3508.               /* Newline in a file.  Count it.  */
  3509.               ++ip->lineno;
  3510.               ++op->lineno;
  3511.             } else if (!output_marks) {
  3512.               /* A newline mark, and we don't want marks
  3513.                  in the output.  If it is newline-hyphen,
  3514.                  discard it entirely.  Otherwise, it is
  3515.                  newline-whitechar, so keep the whitechar.  */
  3516.               obp--;
  3517.               if (*ibp == '-')
  3518.                 ibp++;
  3519.               else {
  3520.                 if (*ibp == '\n')
  3521.                   ++op->lineno;
  3522.                 *obp++ = *ibp++;
  3523.               }
  3524.             } else {
  3525.               /* A newline mark; copy both chars to the output.  */
  3526.               *obp++ = *ibp++;
  3527.             }
  3528.               }
  3529.             }
  3530.             else break;
  3531.           }
  3532.           if (*ibp != '(') {
  3533.             /* It isn't a macro call.
  3534.                Put back the space that we just skipped.  */
  3535.             ibp = old_ibp;
  3536.             obp = old_obp;
  3537.             ip->lineno = old_iln;
  3538.             op->lineno = old_oln;
  3539.             /* Exit the for loop.  */
  3540.             break;
  3541.           }
  3542.         }
  3543.           
  3544.           /* This is now known to be a macro call.
  3545.          Discard the macro name from the output,
  3546.          along with any following whitespace just copied,
  3547.          but preserve newlines if not outputting marks since this
  3548.          is more likely to do the right thing with line numbers.  */
  3549.           obp = op->buf + obufp_before_macroname;
  3550.           if (output_marks)
  3551.         op->lineno = op_lineno_before_macroname;
  3552.           else {
  3553.         int newlines = op->lineno - op_lineno_before_macroname;
  3554.         while (0 < newlines--)
  3555.           *obp++ = '\n';
  3556.           }
  3557.  
  3558.           /* Prevent accidental token-pasting with a character
  3559.          before the macro call.  */
  3560.           if (!traditional && obp != op->buf) {
  3561.         switch (obp[-1]) {
  3562.         case '!':  case '%':  case '&':  case '*':
  3563.         case '+':  case '-':  case '/':  case ':':
  3564.         case '<':  case '=':  case '>':  case '^':
  3565.         case '|':
  3566.           /* If we are expanding a macro arg, make a newline marker
  3567.              to separate the tokens.  If we are making real output,
  3568.              a plain space will do.  */
  3569.           if (output_marks)
  3570.             *obp++ = '\n';
  3571.           *obp++ = ' ';
  3572.         }
  3573.           }
  3574.  
  3575.           /* Expand the macro, reading arguments as needed,
  3576.          and push the expansion on the input stack.  */
  3577.           ip->bufp = ibp;
  3578.           op->bufp = obp;
  3579.           macroexpand (hp, op);
  3580.           
  3581.           /* Reexamine input stack, since macroexpand has pushed
  3582.          a new level on it.  */
  3583.           obp = op->bufp;
  3584.           RECACHE;
  3585.           break;
  3586.         }
  3587. hashcollision:
  3588.         ;
  3589.       }            /* End hash-table-search loop */
  3590.     }
  3591.     ident_length = hash = 0; /* Stop collecting identifier */
  3592.     redo_char = 0;
  3593.     concatenated = 0;
  3594.       }                /* End if (ident_length > 0) */
  3595.     }                /* End switch */
  3596.   }                /* End per-char loop */
  3597.  
  3598.   /* Come here to return -- but first give an error message
  3599.      if there was an unterminated successful conditional.  */
  3600.  ending:
  3601.   if (if_stack != ip->if_stack)
  3602.     {
  3603.       char *str;
  3604.  
  3605.       switch (if_stack->type)
  3606.     {
  3607.     case T_IF:
  3608.       str = "if";
  3609.       break;
  3610.     case T_IFDEF:
  3611.       str = "ifdef";
  3612.       break;
  3613.     case T_IFNDEF:
  3614.       str = "ifndef";
  3615.       break;
  3616.     case T_ELSE:
  3617.       str = "else";
  3618.       break;
  3619.     case T_ELIF:
  3620.       str = "elif";
  3621.       break;
  3622.     default:
  3623.       abort ();
  3624.     }
  3625.  
  3626.       error_with_line (line_for_error (if_stack->lineno),
  3627.                "unterminated `#%s' conditional", str);
  3628.   }
  3629.   if_stack = ip->if_stack;
  3630. }
  3631.  
  3632. /*
  3633.  * Rescan a string into a temporary buffer and return the result
  3634.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  3635.  *
  3636.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  3637.  * and insert such markers when appropriate.  See `rescan' for details.
  3638.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  3639.  * before substitution; it is 0 for other uses.
  3640.  */
  3641. static FILE_BUF
  3642. expand_to_temp_buffer (buf, limit, output_marks, assertions)
  3643.      U_CHAR *buf, *limit;
  3644.      int output_marks, assertions;
  3645. {
  3646.   register FILE_BUF *ip;
  3647.   FILE_BUF obuf;
  3648.   int length = limit - buf;
  3649.   U_CHAR *buf1;
  3650.   int odepth = indepth;
  3651.   int save_assertions_flag = assertions_flag;
  3652.  
  3653.   assertions_flag = assertions;
  3654.  
  3655.   if (length < 0)
  3656.     abort ();
  3657.  
  3658.   /* Set up the input on the input stack.  */
  3659.  
  3660.   buf1 = (U_CHAR *) alloca (length + 1);
  3661.   {
  3662.     register U_CHAR *p1 = buf;
  3663.     register U_CHAR *p2 = buf1;
  3664.  
  3665.     while (p1 != limit)
  3666.       *p2++ = *p1++;
  3667.   }
  3668.   buf1[length] = 0;
  3669.  
  3670.   /* Set up to receive the output.  */
  3671.  
  3672.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  3673.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  3674.   obuf.fname = 0;
  3675.   obuf.macro = 0;
  3676.   obuf.free_ptr = 0;
  3677.  
  3678.   CHECK_DEPTH ({return obuf;});
  3679.  
  3680.   ++indepth;
  3681.  
  3682.   ip = &instack[indepth];
  3683.   ip->fname = 0;
  3684.   ip->nominal_fname = 0;
  3685.   ip->system_header_p = 0;
  3686.   ip->macro = 0;
  3687.   ip->free_ptr = 0;
  3688.   ip->length = length;
  3689.   ip->buf = ip->bufp = buf1;
  3690.   ip->if_stack = if_stack;
  3691.  
  3692.   ip->lineno = obuf.lineno = 1;
  3693.  
  3694.   /* Scan the input, create the output.  */
  3695.   rescan (&obuf, output_marks);
  3696.  
  3697.   /* Pop input stack to original state.  */
  3698.   --indepth;
  3699.  
  3700.   if (indepth != odepth)
  3701.     abort ();
  3702.  
  3703.   /* Record the output.  */
  3704.   obuf.length = obuf.bufp - obuf.buf;
  3705.  
  3706.   assertions_flag = save_assertions_flag;
  3707.   return obuf;
  3708. }
  3709.  
  3710. /*
  3711.  * Process a # directive.  Expects IP->bufp to point after the '#', as in
  3712.  * `#define foo bar'.  Passes to the directive handler
  3713.  * (do_define, do_include, etc.): the addresses of the 1st and
  3714.  * last chars of the directive (starting immediately after the #
  3715.  * keyword), plus op and the keyword table pointer.  If the directive
  3716.  * contains comments it is copied into a temporary buffer sans comments
  3717.  * and the temporary buffer is passed to the directive handler instead.
  3718.  * Likewise for backslash-newlines.
  3719.  *
  3720.  * Returns nonzero if this was a known # directive.
  3721.  * Otherwise, returns zero, without advancing the input pointer.
  3722.  */
  3723.  
  3724. static int
  3725. handle_directive (ip, op)
  3726.      FILE_BUF *ip, *op;
  3727. {
  3728.   register U_CHAR *bp, *cp;
  3729.   register struct directive *kt;
  3730.   register int ident_length;
  3731.   U_CHAR *resume_p;
  3732.  
  3733.   /* Nonzero means we must copy the entire directive
  3734.      to get rid of comments or backslash-newlines.  */
  3735.   int copy_directive = 0;
  3736.  
  3737.   U_CHAR *ident, *after_ident;
  3738.  
  3739.   bp = ip->bufp;
  3740.  
  3741.   /* Record where the directive started.  do_xifdef needs this.  */
  3742.   directive_start = bp - 1;
  3743.  
  3744.   /* Skip whitespace and \-newline.  */
  3745.   while (1) {
  3746.     if (is_hor_space[*bp]) {
  3747.       if (*bp != ' ' && *bp != '\t' && pedantic)
  3748.     pedwarn ("%s in preprocessing directive", char_name[*bp]);
  3749.       bp++;
  3750.     } else if (*bp == '/' && (bp[1] == '*'
  3751.                   || (cplusplus_comments && bp[1] == '/'))) {
  3752.       ip->bufp = bp + 2;
  3753.       skip_to_end_of_comment (ip, &ip->lineno, 0);
  3754.       bp = ip->bufp;
  3755.     } else if (*bp == '\\' && bp[1] == '\n') {
  3756.       bp += 2; ip->lineno++;
  3757.     } else break;
  3758.   }
  3759.  
  3760.   /* Now find end of directive name.
  3761.      If we encounter a backslash-newline, exchange it with any following
  3762.      symbol-constituents so that we end up with a contiguous name.  */
  3763.  
  3764.   cp = bp;
  3765.   while (1) {
  3766.     if (is_idchar[*cp])
  3767.       cp++;
  3768.     else {
  3769.       if (*cp == '\\' && cp[1] == '\n')
  3770.     name_newline_fix (cp);
  3771.       if (is_idchar[*cp])
  3772.     cp++;
  3773.       else break;
  3774.     }
  3775.   }
  3776.   ident_length = cp - bp;
  3777.   ident = bp;
  3778.   after_ident = cp;
  3779.  
  3780.   /* A line of just `#' becomes blank.  */
  3781.  
  3782.   if (ident_length == 0 && *after_ident == '\n') {
  3783.     ip->bufp = after_ident;
  3784.     return 1;
  3785.   }
  3786.  
  3787.   if (ident_length == 0 || !is_idstart[*ident]) {
  3788.     U_CHAR *p = ident;
  3789.     while (is_idchar[*p]) {
  3790.       if (*p < '0' || *p > '9')
  3791.     break;
  3792.       p++;
  3793.     }
  3794.     /* Handle # followed by a line number.  */
  3795.     if (p != ident && !is_idchar[*p]) {
  3796.       static struct directive line_directive_table[] = {
  3797.     {  4, do_line, "line", T_LINE},
  3798.       };
  3799.       if (pedantic)
  3800.     pedwarn ("`#' followed by integer");
  3801.       after_ident = ident;
  3802.       kt = line_directive_table;
  3803.       goto old_linenum;
  3804.     }
  3805.  
  3806.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  3807.     if (p == ident) {
  3808.       while (*p == '#' || is_hor_space[*p]) p++;
  3809.       if (*p == '\n') {
  3810.     if (pedantic && !lang_asm)
  3811.       warning ("invalid preprocessing directive");
  3812.     return 0;
  3813.       }
  3814.     }
  3815.  
  3816.     if (!lang_asm)
  3817.       error ("invalid preprocessing directive name");
  3818.  
  3819.     return 0;
  3820.   }
  3821.  
  3822.   /*
  3823.    * Decode the keyword and call the appropriate expansion
  3824.    * routine, after moving the input pointer up to the next line.
  3825.    */
  3826.   for (kt = directive_table; kt->length > 0; kt++) {
  3827.     if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) {
  3828.       register U_CHAR *buf;
  3829.       register U_CHAR *limit;
  3830.       int unterminated;
  3831.       int junk;
  3832.       int *already_output;
  3833.  
  3834.       /* Nonzero means do not delete comments within the directive.
  3835.      #define needs this when -traditional.  */
  3836.       int keep_comments;
  3837.  
  3838.     old_linenum:
  3839.  
  3840.       limit = ip->buf + ip->length;
  3841.       unterminated = 0;
  3842.       already_output = 0;
  3843.       keep_comments = traditional && kt->traditional_comments;
  3844.       /* #import is defined only in Objective C, or when on the NeXT.  */
  3845.       if (kt->type == T_IMPORT
  3846.       && !(objc || lookup ((U_CHAR *) "__NeXT__", -1, -1)))
  3847.     break;
  3848.  
  3849.       /* Find the end of this directive (first newline not backslashed
  3850.      and not in a string or comment).
  3851.      Set COPY_DIRECTIVE if the directive must be copied
  3852.      (it contains a backslash-newline or a comment).  */
  3853.  
  3854.       buf = bp = after_ident;
  3855.       while (bp < limit) {
  3856.     register U_CHAR c = *bp++;
  3857.     switch (c) {
  3858.     case '\\':
  3859.       if (bp < limit) {
  3860.         if (*bp == '\n') {
  3861.           ip->lineno++;
  3862.           copy_directive = 1;
  3863.           bp++;
  3864.         } else if (traditional)
  3865.           bp++;
  3866.       }
  3867.       break;
  3868.  
  3869.     case '\'':
  3870.     case '\"':
  3871.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_directive, &unterminated);
  3872.       /* Don't bother calling the directive if we already got an error
  3873.          message due to unterminated string.  Skip everything and pretend
  3874.          we called the directive.  */
  3875.       if (unterminated) {
  3876.         if (traditional) {
  3877.           /* Traditional preprocessing permits unterminated strings.  */
  3878.           ip->bufp = bp;
  3879.           goto endloop1;
  3880.         }
  3881.         ip->bufp = bp;
  3882.         return 1;
  3883.       }
  3884.       break;
  3885.  
  3886.       /* <...> is special for #include.  */
  3887.     case '<':
  3888.       if (!kt->angle_brackets)
  3889.         break;
  3890.       while (bp < limit && *bp != '>' && *bp != '\n') {
  3891.         if (*bp == '\\' && bp[1] == '\n') {
  3892.           ip->lineno++;
  3893.           copy_directive = 1;
  3894.           bp++;
  3895.         }
  3896.         bp++;
  3897.       }
  3898.       break;
  3899.  
  3900.     case '/':
  3901.       if (*bp == '\\' && bp[1] == '\n')
  3902.         newline_fix (bp);
  3903.       if (*bp == '*'
  3904.           || (cplusplus_comments && *bp == '/')) {
  3905.         U_CHAR *obp = bp - 1;
  3906.         ip->bufp = bp + 1;
  3907.         skip_to_end_of_comment (ip, &ip->lineno, 0);
  3908.         bp = ip->bufp;
  3909.         /* No need to copy the directive because of a comment at the end;
  3910.            just don't include the comment in the directive.  */
  3911.         if (bp == limit || *bp == '\n') {
  3912.           bp = obp;
  3913.           goto endloop1;
  3914.         }
  3915.         /* Don't remove the comments if -traditional.  */
  3916.         if (! keep_comments)
  3917.           copy_directive++;
  3918.       }
  3919.       break;
  3920.  
  3921.     case '\f':
  3922.     case '\r':
  3923.     case '\v':
  3924.       if (pedantic)
  3925.         pedwarn ("%s in preprocessing directive", char_name[c]);
  3926.       break;
  3927.  
  3928.     case '\n':
  3929.       --bp;        /* Point to the newline */
  3930.       ip->bufp = bp;
  3931.       goto endloop1;
  3932.     }
  3933.       }
  3934.       ip->bufp = bp;
  3935.  
  3936.     endloop1:
  3937.       resume_p = ip->bufp;
  3938.       /* BP is the end of the directive.
  3939.      RESUME_P is the next interesting data after the directive.
  3940.      A comment may come between.  */
  3941.  
  3942.       /* If a directive should be copied through, and -E was given,
  3943.      pass it through before removing comments.  */
  3944.       if (!no_output && kt->pass_thru && put_out_comments) {
  3945.         int len;
  3946.  
  3947.     /* Output directive name.  */
  3948.         check_expand (op, kt->length + 2);
  3949.     /* Make sure # is at the start of a line */
  3950.     if (op->bufp > op->buf && op->bufp[-1] != '\n') {
  3951.       op->lineno++;
  3952.       *op->bufp++ = '\n';
  3953.     }
  3954.         *op->bufp++ = '#';
  3955.         bcopy (kt->name, op->bufp, kt->length);
  3956.         op->bufp += kt->length;
  3957.  
  3958.     /* Output arguments.  */
  3959.     len = (bp - buf);
  3960.     check_expand (op, len);
  3961.     bcopy (buf, (char *) op->bufp, len);
  3962.     op->bufp += len;
  3963.     /* Take account of any (escaped) newlines just output.  */
  3964.     while (--len >= 0)
  3965.       if (buf[len] == '\n')
  3966.         op->lineno++;
  3967.  
  3968.     already_output = &junk;
  3969.       }                /* Don't we need a newline or #line? */
  3970.  
  3971.       if (copy_directive) {
  3972.     register U_CHAR *xp = buf;
  3973.     /* Need to copy entire directive into temp buffer before dispatching */
  3974.  
  3975.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for directive plus
  3976.                           some slop */
  3977.     buf = cp;
  3978.  
  3979.     /* Copy to the new buffer, deleting comments
  3980.        and backslash-newlines (and whitespace surrounding the latter).  */
  3981.  
  3982.     while (xp < bp) {
  3983.       register U_CHAR c = *xp++;
  3984.       *cp++ = c;
  3985.  
  3986.       switch (c) {
  3987.       case '\n':
  3988.         abort ();  /* A bare newline should never part of the line.  */
  3989.         break;
  3990.  
  3991.         /* <...> is special for #include.  */
  3992.       case '<':
  3993.         if (!kt->angle_brackets)
  3994.           break;
  3995.         while (xp < bp && c != '>') {
  3996.           c = *xp++;
  3997.           if (c == '\\' && xp < bp && *xp == '\n')
  3998.         xp++;
  3999.           else
  4000.         *cp++ = c;
  4001.         }
  4002.         break;
  4003.  
  4004.       case '\\':
  4005.         if (*xp == '\n') {
  4006.           xp++;
  4007.           cp--;
  4008.           if (cp != buf && is_space[cp[-1]]) {
  4009.         while (cp != buf && is_space[cp[-1]]) cp--;
  4010.         cp++;
  4011.         SKIP_WHITE_SPACE (xp);
  4012.           } else if (is_space[*xp]) {
  4013.         *cp++ = *xp++;
  4014.         SKIP_WHITE_SPACE (xp);
  4015.           }
  4016.         } else if (traditional && xp < bp) {
  4017.           *cp++ = *xp++;
  4018.         }
  4019.         break;
  4020.  
  4021.       case '\'':
  4022.       case '\"':
  4023.         {
  4024.           register U_CHAR *bp1
  4025.         = skip_quoted_string (xp - 1, bp, ip->lineno,
  4026.                       NULL_PTR, NULL_PTR, NULL_PTR);
  4027.           while (xp != bp1)
  4028.         if (*xp == '\\') {
  4029.           if (*++xp != '\n')
  4030.             *cp++ = '\\';
  4031.           else
  4032.             xp++;
  4033.         } else
  4034.           *cp++ = *xp++;
  4035.         }
  4036.         break;
  4037.  
  4038.       case '/':
  4039.         if (*xp == '*'
  4040.         || (cplusplus_comments && *xp == '/')) {
  4041.           ip->bufp = xp + 1;
  4042.           /* If we already copied the directive through,
  4043.          already_output != 0 prevents outputting comment now.  */
  4044.           skip_to_end_of_comment (ip, already_output, 0);
  4045.           if (keep_comments)
  4046.         while (xp != ip->bufp)
  4047.           *cp++ = *xp++;
  4048.           /* Delete or replace the slash.  */
  4049.           else if (traditional)
  4050.         cp--;
  4051.           else
  4052.         cp[-1] = ' ';
  4053.           xp = ip->bufp;
  4054.         }
  4055.       }
  4056.     }
  4057.  
  4058.     /* Null-terminate the copy.  */
  4059.  
  4060.     *cp = 0;
  4061.       } else
  4062.     cp = bp;
  4063.  
  4064.       ip->bufp = resume_p;
  4065.  
  4066.       /* Some directives should be written out for cc1 to process,
  4067.      just as if they were not defined.  And sometimes we're copying
  4068.      definitions through.  */
  4069.  
  4070.       if (!no_output && already_output == 0
  4071.       && (kt->pass_thru
  4072. /* Phil.B 27-Mar-93 not quite sure to keep this old fix */      
  4073. #if defined (__amigados__) && 0
  4074.           || ((kt->type == T_DEFINE || kt->type == T_UNDEF)
  4075. #else
  4076.           || (kt->type == T_DEFINE
  4077. #endif /* __amigados__ */
  4078.           && (dump_macros == dump_names
  4079.               || dump_macros == dump_definitions)))) {
  4080.         int len;
  4081.  
  4082.     /* Output directive name.  */
  4083.         check_expand (op, kt->length + 1);
  4084.         *op->bufp++ = '#';
  4085.         bcopy (kt->name, (char *) op->bufp, kt->length);
  4086.         op->bufp += kt->length;
  4087.  
  4088.     if (kt->pass_thru || dump_macros == dump_definitions) {
  4089.       /* Output arguments.  */
  4090.       len = (cp - buf);
  4091.       check_expand (op, len);
  4092.       bcopy (buf, (char *) op->bufp, len);
  4093.       op->bufp += len;
  4094.     } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
  4095.       U_CHAR *xp = buf;
  4096.       U_CHAR *yp;
  4097.       SKIP_WHITE_SPACE (xp);
  4098.       yp = xp;
  4099.       while (is_idchar[*xp]) xp++;
  4100.       len = (xp - yp);
  4101.       check_expand (op, len + 1);
  4102.       *op->bufp++ = ' ';
  4103.       bcopy (yp, op->bufp, len);
  4104.       op->bufp += len;
  4105.     }
  4106.       }                /* Don't we need a newline or #line? */
  4107.  
  4108.       /* Call the appropriate directive handler.  buf now points to
  4109.      either the appropriate place in the input buffer, or to
  4110.      the temp buffer if it was necessary to make one.  cp
  4111.      points to the first char after the contents of the (possibly
  4112.      copied) directive, in either case. */
  4113.       (*kt->func) (buf, cp, op, kt);
  4114.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  4115.  
  4116.       return 1;
  4117.     }
  4118.   }
  4119.  
  4120.   /* It is deliberate that we don't warn about undefined directives.
  4121.      That is the responsibility of cc1.  */
  4122.   return 0;
  4123. }
  4124.  
  4125. static struct tm *
  4126. timestamp ()
  4127. {
  4128.   static struct tm *timebuf;
  4129.   if (!timebuf) {
  4130.     time_t t = time ((time_t *)0);
  4131.     timebuf = localtime (&t);
  4132.   }
  4133.   return timebuf;
  4134. }
  4135.  
  4136. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  4137.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  4138.                 };
  4139.  
  4140. /*
  4141.  * expand things like __FILE__.  Place the expansion into the output
  4142.  * buffer *without* rescanning.
  4143.  */
  4144.  
  4145. static void
  4146. special_symbol (hp, op)
  4147.      HASHNODE *hp;
  4148.      FILE_BUF *op;
  4149. {
  4150.   char *buf;
  4151.   int i, len;
  4152.   int true_indepth;
  4153.   FILE_BUF *ip = NULL;
  4154.   struct tm *timebuf;
  4155.  
  4156.   int paren = 0;        /* For special `defined' keyword */
  4157.  
  4158.   if (pcp_outfile && pcp_inside_if
  4159.       && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
  4160.     error ("Predefined macro `%s' used inside `#if' during precompilation",
  4161.        hp->name);
  4162.     
  4163.   for (i = indepth; i >= 0; i--)
  4164.     if (instack[i].fname != NULL) {
  4165.       ip = &instack[i];
  4166.       break;
  4167.     }
  4168.   if (ip == NULL) {
  4169.     error ("cccp error: not in any file?!");
  4170.     return;            /* the show must go on */
  4171.   }
  4172.  
  4173.   switch (hp->type) {
  4174.   case T_FILE:
  4175.   case T_BASE_FILE:
  4176.     {
  4177.       char *string;
  4178.       if (hp->type == T_FILE)
  4179.     string = ip->nominal_fname;
  4180.       else
  4181.     string = instack[0].nominal_fname;
  4182.  
  4183.       if (string)
  4184.     {
  4185.       buf = (char *) alloca (3 + 4 * strlen (string));
  4186.       quote_string (buf, string);
  4187.     }
  4188.       else
  4189.     buf = "\"\"";
  4190.  
  4191.       break;
  4192.     }
  4193.  
  4194.   case T_INCLUDE_LEVEL:
  4195.     true_indepth = 0;
  4196.     for (i = indepth; i >= 0; i--)
  4197.       if (instack[i].fname != NULL)
  4198.         true_indepth++;
  4199.  
  4200.     buf = (char *) alloca (8);    /* Eight bytes ought to be more than enough */
  4201.     sprintf (buf, "%d", true_indepth - 1);
  4202.     break;
  4203.  
  4204.   case T_VERSION:
  4205.     buf = (char *) alloca (3 + strlen (version_string));
  4206.     sprintf (buf, "\"%s\"", version_string);
  4207.     break;
  4208.  
  4209. #ifndef NO_BUILTIN_SIZE_TYPE
  4210.   case T_SIZE_TYPE:
  4211.     buf = SIZE_TYPE;
  4212.     break;
  4213. #endif
  4214.  
  4215. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  4216.   case T_PTRDIFF_TYPE:
  4217.     buf = PTRDIFF_TYPE;
  4218.     break;
  4219. #endif
  4220.  
  4221.   case T_WCHAR_TYPE:
  4222.     buf = wchar_type;
  4223.     break;
  4224.  
  4225.   case T_USER_LABEL_PREFIX_TYPE:
  4226.     buf = USER_LABEL_PREFIX;
  4227.     break;
  4228.  
  4229.   case T_REGISTER_PREFIX_TYPE:
  4230.     buf = REGISTER_PREFIX;
  4231.     break;
  4232.  
  4233.   case T_IMMEDIATE_PREFIX_TYPE:
  4234.     buf = IMMEDIATE_PREFIX;
  4235.     break;
  4236.  
  4237.   case T_CONST:
  4238.     buf = hp->value.cpval;
  4239.     if (pcp_inside_if && pcp_outfile)
  4240.       /* Output a precondition for this macro use */
  4241.       fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf);
  4242.     break;
  4243.  
  4244.   case T_SPECLINE:
  4245.     buf = (char *) alloca (10);
  4246.     sprintf (buf, "%d", ip->lineno);
  4247.     break;
  4248.  
  4249.   case T_DATE:
  4250.   case T_TIME:
  4251.     buf = (char *) alloca (20);
  4252.     timebuf = timestamp ();
  4253.     if (hp->type == T_DATE)
  4254.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  4255.           timebuf->tm_mday, timebuf->tm_year + 1900);
  4256.     else
  4257.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  4258.           timebuf->tm_sec);
  4259.     break;
  4260.  
  4261.   case T_SPEC_DEFINED:
  4262.     buf = " 0 ";        /* Assume symbol is not defined */
  4263.     ip = &instack[indepth];
  4264.     SKIP_WHITE_SPACE (ip->bufp);
  4265.     if (*ip->bufp == '(') {
  4266.       paren++;
  4267.       ip->bufp++;            /* Skip over the paren */
  4268.       SKIP_WHITE_SPACE (ip->bufp);
  4269.     }
  4270.  
  4271.     if (!is_idstart[*ip->bufp])
  4272.       goto oops;
  4273.     if ((hp = lookup (ip->bufp, -1, -1))) {
  4274.       if (pcp_outfile && pcp_inside_if
  4275.       && (hp->type == T_CONST
  4276.           || (hp->type == T_MACRO && hp->value.defn->predefined)))
  4277.     /* Output a precondition for this macro use. */
  4278.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  4279.       buf = " 1 ";
  4280.     }
  4281.     else
  4282.       if (pcp_outfile && pcp_inside_if)    {
  4283.     /* Output a precondition for this macro use */
  4284.     U_CHAR *cp = ip->bufp;
  4285.     fprintf (pcp_outfile, "#undef ");
  4286.     while (is_idchar[*cp]) /* Ick! */
  4287.       fputc (*cp++, pcp_outfile);
  4288.     putc ('\n', pcp_outfile);
  4289.       }
  4290.     while (is_idchar[*ip->bufp])
  4291.       ++ip->bufp;
  4292.     SKIP_WHITE_SPACE (ip->bufp);
  4293.     if (paren) {
  4294.       if (*ip->bufp != ')')
  4295.     goto oops;
  4296.       ++ip->bufp;
  4297.     }
  4298.     break;
  4299.  
  4300. oops:
  4301.  
  4302.     error ("`defined' without an identifier");
  4303.     break;
  4304.  
  4305.   default:
  4306.     error ("cccp error: invalid special hash type"); /* time for gdb */
  4307.     abort ();
  4308.   }
  4309.   len = strlen (buf);
  4310.   check_expand (op, len);
  4311.   bcopy (buf, (char *) op->bufp, len);
  4312.   op->bufp += len;
  4313.  
  4314.   return;
  4315. }
  4316.  
  4317.  
  4318. /* Routines to handle #directives */
  4319.  
  4320. /* Handle #include and #import.
  4321.    This function expects to see "fname" or <fname> on the input.  */
  4322.  
  4323. static int
  4324. do_include (buf, limit, op, keyword)
  4325.      U_CHAR *buf, *limit;
  4326.      FILE_BUF *op;
  4327.      struct directive *keyword;
  4328. {
  4329.   int importing = (keyword->type == T_IMPORT);
  4330.   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
  4331.   static int import_warning = 0;
  4332.   char *fname;        /* Dynamically allocated fname buffer */
  4333.   char *pcftry;
  4334.   char *pcfname;
  4335.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  4336.  
  4337.   struct file_name_list *search_start = include; /* Chain of dirs to search */
  4338.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  4339.   struct file_name_list *searchptr = 0;
  4340.   int flen;
  4341.  
  4342.   int f;            /* file number */
  4343.  
  4344.   int retried = 0;        /* Have already tried macro
  4345.                    expanding the include line*/
  4346.   int angle_brackets = 0;    /* 0 for "...", 1 for <...> */
  4347.   int pcf = -1;
  4348.   char *pcfbuf;
  4349.   char *pcfbuflimit;
  4350.   int pcfnum;
  4351.   f= -1;            /* JF we iz paranoid! */
  4352.  
  4353.   if (importing && warn_import && !inhibit_warnings
  4354.       && !instack[indepth].system_header_p && !import_warning) {
  4355.     import_warning = 1;
  4356.     warning ("using `#import' is not recommended");
  4357.     fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
  4358.     fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
  4359.     fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
  4360.     fprintf (stderr, "  #ifndef _FOO_H_INCLUDED\n");
  4361.     fprintf (stderr, "  #define _FOO_H_INCLUDED\n");
  4362.     fprintf (stderr, "  ... <real contents of file> ...\n");
  4363.     fprintf (stderr, "  #endif /* Not _FOO_H_INCLUDED */\n\n");
  4364.     fprintf (stderr, "Then users can use `#include' any number of times.\n");
  4365.     fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
  4366.     fprintf (stderr, "when it is equipped with such a conditional.\n");
  4367.   }
  4368.  
  4369. get_filename:
  4370.  
  4371.   fbeg = buf;
  4372.   SKIP_WHITE_SPACE (fbeg);
  4373.   /* Discard trailing whitespace so we can easily see
  4374.      if we have parsed all the significant chars we were given.  */
  4375.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  4376.  
  4377.   switch (*fbeg++) {
  4378.   case '\"':
  4379.     {
  4380.       FILE_BUF *fp;
  4381.       /* Copy the operand text, concatenating the strings.  */
  4382.       {
  4383.     U_CHAR *fin = fbeg;
  4384.     fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
  4385.     fend = fbeg;
  4386.     while (fin != limit) {
  4387.       while (fin != limit && *fin != '\"')
  4388.         *fend++ = *fin++;
  4389.       fin++;
  4390.       if (fin == limit)
  4391.         break;
  4392.       /* If not at the end, there had better be another string.  */
  4393.       /* Skip just horiz space, and don't go past limit.  */
  4394.       while (fin != limit && is_hor_space[*fin]) fin++;
  4395.       if (fin != limit && *fin == '\"')
  4396.         fin++;
  4397.       else
  4398.         goto fail;
  4399.     }
  4400.       }
  4401.       *fend = 0;
  4402.  
  4403.       /* We have "filename".  Figure out directory this source
  4404.      file is coming from and put it on the front of the list. */
  4405.  
  4406.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  4407.       if (ignore_srcdir) break;
  4408.  
  4409.       for (fp = &instack[indepth]; fp >= instack; fp--)
  4410.     {
  4411.       int n;
  4412.       char *ep,*nam;
  4413.  
  4414.       if ((nam = fp->nominal_fname) != NULL) {
  4415.         /* Found a named file.  Figure out dir of the file,
  4416.            and put it in front of the search list.  */
  4417.         dsp[0].next = search_start;
  4418.         search_start = dsp;
  4419. #ifndef VMS
  4420.         ep = rindex (nam, '/');
  4421. #ifdef DIR_SEPARATOR
  4422.         if (ep == NULL) ep = rindex (nam, DIR_SEPARATOR);
  4423.         else {
  4424.           char *tmp = rindex (nam, DIR_SEPARATOR);
  4425.           if (tmp != NULL && tmp > ep) ep = tmp;
  4426.         }
  4427. #endif
  4428. #ifdef VOL_SEPARATOR
  4429.         if (ep == NULL) ep = rindex (nam, VOL_SEPARATOR);
  4430.         else {
  4431.           char *tmp = rindex (nam, VOL_SEPARATOR);
  4432.           if (tmp != NULL && tmp > ep) ep = tmp;
  4433.         }
  4434.         if (ep != NULL && *ep == VOL_SEPARATOR) ep++;
  4435. #endif
  4436. #else                /* VMS */
  4437.         ep = rindex (nam, ']');
  4438.         if (ep == NULL) ep = rindex (nam, '>');
  4439.         if (ep == NULL) ep = rindex (nam, ':');
  4440.         if (ep != NULL) ep++;
  4441. #endif                /* VMS */
  4442.         if (ep != NULL) {
  4443.           n = ep - nam;
  4444.           dsp[0].fname = (char *) alloca (n + 1);
  4445.           strncpy (dsp[0].fname, nam, n);
  4446.           dsp[0].fname[n] = '\0';
  4447.           if (n + INCLUDE_LEN_FUDGE > max_include_len)
  4448.         max_include_len = n + INCLUDE_LEN_FUDGE;
  4449.         } else {
  4450.           dsp[0].fname = 0; /* Current directory */
  4451.         }
  4452.         dsp[0].got_name_map = 0;
  4453.         break;
  4454.       }
  4455.     }
  4456.       break;
  4457.     }
  4458.  
  4459.   case '<':
  4460.     fend = fbeg;
  4461.     while (fend != limit && *fend != '>') fend++;
  4462.     if (*fend == '>' && fend + 1 == limit) {
  4463.       angle_brackets = 1;
  4464.       /* If -I-, start with the first -I dir after the -I-.  */
  4465.       if (first_bracket_include)
  4466.     search_start = first_bracket_include;
  4467.       break;
  4468.     }
  4469.     goto fail;
  4470.  
  4471.   default:
  4472. #ifdef VMS
  4473.     /*
  4474.      * Support '#include xyz' like VAX-C to allow for easy use of all the
  4475.      * decwindow include files. It defaults to '#include <xyz.h>' (so the
  4476.      * code from case '<' is repeated here) and generates a warning.
  4477.      * (Note: macro expansion of `xyz' takes precedence.)
  4478.      */
  4479.     if (retried && isalpha(*(--fbeg))) {
  4480.       fend = fbeg;
  4481.       while (fend != limit && (!isspace(*fend))) fend++;
  4482.       warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
  4483.       if (fend  == limit) {
  4484.     angle_brackets = 1;
  4485.     /* If -I-, start with the first -I dir after the -I-.  */
  4486.     if (first_bracket_include)
  4487.       search_start = first_bracket_include;
  4488.     break;
  4489.       }
  4490.     }
  4491. #endif
  4492.  
  4493.   fail:
  4494.     if (retried) {
  4495.       error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
  4496.       return 0;
  4497.     } else {
  4498.       /* Expand buffer and then remove any newline markers.
  4499.      We can't just tell expand_to_temp_buffer to omit the markers,
  4500.      since it would put extra spaces in include file names.  */
  4501.       FILE_BUF trybuf;
  4502.       U_CHAR *src;
  4503.       trybuf = expand_to_temp_buffer (buf, limit, 1, 0);
  4504.       src = trybuf.buf;
  4505.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  4506.       limit = buf;
  4507.       while (src != trybuf.bufp) {
  4508.     switch ((*limit++ = *src++)) {
  4509.       case '\n':
  4510.         limit--;
  4511.         src++;
  4512.         break;
  4513.  
  4514.       case '\'':
  4515.       case '\"':
  4516.         {
  4517.           U_CHAR *src1 = skip_quoted_string (src - 1, trybuf.bufp, 0,
  4518.                          NULL_PTR, NULL_PTR, NULL_PTR);
  4519.           while (src != src1)
  4520.         *limit++ = *src++;
  4521.         }
  4522.         break;
  4523.     }
  4524.       }
  4525.       *limit = 0;
  4526.       free (trybuf.buf);
  4527.       retried++;
  4528.       goto get_filename;
  4529.     }
  4530.   }
  4531.  
  4532.   /* For #include_next, skip in the search path
  4533.      past the dir in which the containing file was found.  */
  4534.   if (skip_dirs) {
  4535.     FILE_BUF *fp;
  4536.     for (fp = &instack[indepth]; fp >= instack; fp--)
  4537.       if (fp->fname != NULL) {
  4538.     /* fp->dir is null if the containing file was specified
  4539.        with an absolute file name.  In that case, don't skip anything.  */
  4540.     if (fp->dir)
  4541.       search_start = fp->dir->next;
  4542.     break;
  4543.       }
  4544.   }
  4545.  
  4546.   flen = fend - fbeg;
  4547.  
  4548.   if (flen == 0)
  4549.     {
  4550.       error ("empty file name in `#%s'", keyword->name);
  4551.       return 0;
  4552.     }
  4553.  
  4554.   /* Allocate this permanently, because it gets stored in the definitions
  4555.      of macros.  */
  4556.   fname = xmalloc (max_include_len + flen + 4);
  4557.   /* + 2 above for slash and terminating null.  */
  4558.   /* + 2 added for '.h' on VMS (to support '#include filename') */
  4559.  
  4560.   /* If specified file name is absolute, just open it.  */
  4561.  
  4562. #ifdef __amigados__
  4563.   if (amigados_abs_filename (fbeg, flen)) {
  4564. #else
  4565.   if (*fbeg == '/'
  4566. #ifdef DIR_SEPARATOR
  4567.       || *fbeg == DIR_SEPARATOR
  4568. #endif
  4569.       ) {
  4570. #endif
  4571.     strncpy (fname, (char *) fbeg, flen);
  4572.     fname[flen] = 0;
  4573.     if (redundant_include_p (fname))
  4574.       return 0;
  4575.     if (importing)
  4576.       f = lookup_import (fname, NULL_PTR);
  4577.     else
  4578.       f = open_include_file (fname, NULL_PTR);
  4579.     if (f == -2)
  4580.       return 0;        /* Already included this file */
  4581.   } else {
  4582.     /* Search directory path, trying to open the file.
  4583.        Copy each filename tried into FNAME.  */
  4584.  
  4585.     for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
  4586.       if (searchptr->fname) {
  4587.     /* The empty string in a search path is ignored.
  4588.        This makes it possible to turn off entirely
  4589.        a standard piece of the list.  */
  4590.     if (searchptr->fname[0] == 0)
  4591.       continue;
  4592.     strcpy (fname, searchptr->fname);
  4593. #ifdef __amigados__
  4594.     if (fname[strlen (fname) - 1] != ':')
  4595. #endif
  4596.     strcat (fname, "/");
  4597.     fname[strlen (fname) + flen] = 0;
  4598.       } else {
  4599.     fname[0] = 0;
  4600.       }
  4601.       strncat (fname, (char *) fbeg, flen);
  4602. #ifdef VMS
  4603.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  4604.          full VMS file specification */
  4605.       if (searchptr->fname && (searchptr->fname[0] != 0)) {
  4606.     /* Fix up the filename */
  4607.     hack_vms_include_specification (fname);
  4608.       } else {
  4609.           /* This is a normal VMS filespec, so use it unchanged.  */
  4610.     strncpy (fname, fbeg, flen);
  4611.     fname[flen] = 0;
  4612.     /* if it's '#include filename', add the missing .h */
  4613.     if (index(fname,'.')==NULL) {
  4614.       strcat (fname, ".h");
  4615.     }
  4616.       }
  4617. #endif /* VMS */
  4618.       /* ??? There are currently 3 separate mechanisms for avoiding processing
  4619.      of redundant include files: #import, #pragma once, and
  4620.      redundant_include_p.  It would be nice if they were unified.  */
  4621.       if (redundant_include_p (fname))
  4622.     return 0;
  4623.       if (importing)
  4624.     f = lookup_import (fname, searchptr);
  4625.       else
  4626.     f = open_include_file (fname, searchptr);
  4627.       if (f == -2)
  4628.     return 0;            /* Already included this file */
  4629. #ifdef EACCES
  4630.       else if (f == -1 && errno == EACCES)
  4631.     warning ("Header file %s exists, but is not readable", fname);
  4632. #endif
  4633.       if (f >= 0)
  4634.     break;
  4635.     }
  4636.   }
  4637.  
  4638.   if (f < 0) {
  4639.     /* A file that was not found.  */
  4640.  
  4641.     strncpy (fname, (char *) fbeg, flen);
  4642.     fname[flen] = 0;
  4643.     /* If generating dependencies and -MG was specified, we assume missing
  4644.        files are leaf files, living in the same directory as the source file
  4645.        or other similar place; these missing files may be generated from
  4646.        other files and may not exist yet (eg: y.tab.h).  */
  4647.     if (print_deps_missing_files
  4648.     && print_deps > (angle_brackets || (system_include_depth > 0)))
  4649.       {
  4650.     /* If it was requested as a system header file,
  4651.        then assume it belongs in the first place to look for such.  */
  4652.     if (angle_brackets)
  4653.       {
  4654.         for (searchptr = search_start; searchptr; searchptr = searchptr->next)
  4655.           {
  4656.         if (searchptr->fname)
  4657.           {
  4658.             char *p;
  4659.  
  4660.             if (searchptr->fname[0] == 0)
  4661.               continue;
  4662.             p = (char *) alloca (strlen (searchptr->fname)
  4663.                      + strlen (fname) + 2);
  4664.             strcpy (p, searchptr->fname);
  4665.             strcat (p, "/");
  4666.             strcat (p, fname);
  4667.             deps_output (p, ' ');
  4668.             break;
  4669.           }
  4670.           }
  4671.       }
  4672.     else
  4673.       {
  4674.         /* Otherwise, omit the directory, as if the file existed
  4675.            in the directory with the source.  */
  4676.         deps_output (fname, ' ');
  4677.       }
  4678.       }
  4679.     /* If -M was specified, and this header file won't be added to the
  4680.        dependency list, then don't count this as an error, because we can
  4681.        still produce correct output.  Otherwise, we can't produce correct
  4682.        output, because there may be dependencies we need inside the missing
  4683.        file, and we don't know what directory this missing file exists in.  */
  4684.     else if (print_deps
  4685.     && (print_deps <= (angle_brackets || (system_include_depth > 0))))
  4686.       warning ("No include path in which to find %s", fname);
  4687.     else if (search_start)
  4688.       error_from_errno (fname);
  4689.     else
  4690.       error ("No include path in which to find %s", fname);
  4691.   } else {
  4692.     /* Check to see if this include file is a once-only include file.
  4693.        If so, give up.  */
  4694.  
  4695.     struct file_name_list* ptr;
  4696.  
  4697.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  4698.       if (!strcmp (ptr->fname, fname)) {
  4699.     close (f);
  4700.         return 0;                /* This file was once'd. */
  4701.       }
  4702.     }
  4703.  
  4704.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  4705.       if (!strcmp (ptr->fname, fname))
  4706.         break;                /* This file was included before. */
  4707.     }
  4708.  
  4709.     if (ptr == 0) {
  4710.       /* This is the first time for this file.  */
  4711.       /* Add it to list of files included.  */
  4712.  
  4713.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  4714.       ptr->control_macro = 0;
  4715.       ptr->c_system_include_path = 0;
  4716.       ptr->next = all_include_files;
  4717.       all_include_files = ptr;
  4718.       ptr->fname = savestring (fname);
  4719.       ptr->got_name_map = 0;
  4720.  
  4721.       /* For -M, add this file to the dependencies.  */
  4722.       if (print_deps > (angle_brackets || (system_include_depth > 0)))
  4723.     deps_output (fname, ' ');
  4724.     }   
  4725.  
  4726.     /* Handle -H option.  */
  4727.     if (print_include_names) {
  4728.       output_dots (stderr, indepth);
  4729.       fprintf (stderr, "%s\n", fname);
  4730.     }
  4731.  
  4732.     if (angle_brackets)
  4733.       system_include_depth++;
  4734.  
  4735.     /* Actually process the file.  */
  4736.     add_import (f, fname);    /* Record file on "seen" list for #import. */
  4737.  
  4738.     pcftry = (char *) alloca (strlen (fname) + 30);
  4739.     pcfbuf = 0;
  4740.     pcfnum = 0;
  4741.  
  4742.     if (!no_precomp)
  4743.       {
  4744.     struct stat stat_f;
  4745.  
  4746.     fstat (f, &stat_f);
  4747.  
  4748.     do {
  4749.       sprintf (pcftry, "%s%d", fname, pcfnum++);
  4750.  
  4751.       pcf = open (pcftry, O_RDONLY, 0666);
  4752.       if (pcf != -1)
  4753.         {
  4754.           struct stat s;
  4755.  
  4756.           fstat (pcf, &s);
  4757.           if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
  4758.             sizeof (s.st_ino))
  4759.           || stat_f.st_dev != s.st_dev)
  4760.         {
  4761.           pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
  4762.           /* Don't need it any more.  */
  4763.           close (pcf);
  4764.         }
  4765.           else
  4766.         {
  4767.           /* Don't need it at all.  */
  4768.           close (pcf);
  4769.           break;
  4770.         }
  4771.         }
  4772.     } while (pcf != -1 && !pcfbuf);
  4773.       }
  4774.     
  4775.     /* Actually process the file */
  4776.     if (pcfbuf) {
  4777.       pcfname = xmalloc (strlen (pcftry) + 1);
  4778.       strcpy (pcfname, pcftry);
  4779.       pcfinclude ((U_CHAR *) pcfbuf, (U_CHAR *) pcfbuflimit,
  4780.           (U_CHAR *) fname, op);
  4781.     }
  4782.     else
  4783.       finclude (f, fname, op, is_system_include (fname), searchptr);
  4784.  
  4785.     if (angle_brackets)
  4786.       system_include_depth--;
  4787.   }
  4788.   return 0;
  4789. }
  4790.  
  4791. /* Return nonzero if there is no need to include file NAME
  4792.    because it has already been included and it contains a conditional
  4793.    to make a repeated include do nothing.  */
  4794.  
  4795. static int
  4796. redundant_include_p (name)
  4797.      char *name;
  4798. {
  4799.   struct file_name_list *l = all_include_files;
  4800.   for (; l; l = l->next)
  4801.     if (! strcmp (name, l->fname)
  4802.     && l->control_macro
  4803.     && lookup (l->control_macro, -1, -1))
  4804.       return 1;
  4805.   return 0;
  4806. }
  4807.  
  4808. /* Return nonzero if the given FILENAME is an absolute pathname which
  4809.    designates a file within one of the known "system" include file
  4810.    directories.  We assume here that if the given FILENAME looks like
  4811.    it is the name of a file which resides either directly in a "system"
  4812.    include file directory, or within any subdirectory thereof, then the
  4813.    given file must be a "system" include file.  This function tells us
  4814.    if we should suppress pedantic errors/warnings for the given FILENAME.
  4815.  
  4816.    The value is 2 if the file is a C-language system header file
  4817.    for which C++ should (on most systems) assume `extern "C"'.  */
  4818.  
  4819. static int
  4820. is_system_include (filename)
  4821.     register char *filename;
  4822. {
  4823.   struct file_name_list *searchptr;
  4824.  
  4825.   for (searchptr = first_system_include; searchptr;
  4826.        searchptr = searchptr->next)
  4827.     if (searchptr->fname) {
  4828.       register char *sys_dir = searchptr->fname;
  4829.       register unsigned length = strlen (sys_dir);
  4830.  
  4831.       if (! strncmp (sys_dir, filename, length)
  4832.       && (filename[length] == '/'
  4833. #ifdef DIR_SEPARATOR
  4834.           || filename[length] == DIR_SEPARATOR
  4835. #endif
  4836.           )) {
  4837.     if (searchptr->c_system_include_path)
  4838.       return 2;
  4839.     else
  4840.       return 1;
  4841.       }
  4842.     }
  4843.   return 0;
  4844. }
  4845.  
  4846. /* The file_name_map structure holds a mapping of file names for a
  4847.    particular directory.  This mapping is read from the file named
  4848.    FILE_NAME_MAP_FILE in that directory.  Such a file can be used to
  4849.    map filenames on a file system with severe filename restrictions,
  4850.    such as DOS.  The format of the file name map file is just a series
  4851.    of lines with two tokens on each line.  The first token is the name
  4852.    to map, and the second token is the actual name to use.  */
  4853.  
  4854. struct file_name_map
  4855. {
  4856.   struct file_name_map *map_next;
  4857.   char *map_from;
  4858.   char *map_to;
  4859. };
  4860.  
  4861. #define FILE_NAME_MAP_FILE "header.gcc"
  4862.  
  4863. /* Read a space delimited string of unlimited length from a stdio
  4864.    file.  */
  4865.  
  4866. static char *
  4867. read_filename_string (ch, f)
  4868.      int ch;
  4869.      FILE *f;
  4870. {
  4871.   char *alloc, *set;
  4872.   int len;
  4873.  
  4874.   len = 20;
  4875.   set = alloc = xmalloc (len + 1);
  4876.   if (! is_space[ch])
  4877.     {
  4878.       *set++ = ch;
  4879.       while ((ch = getc (f)) != EOF && ! is_space[ch])
  4880.     {
  4881.       if (set - alloc == len)
  4882.         {
  4883.           len *= 2;
  4884.           alloc = xrealloc (alloc, len + 1);
  4885.           set = alloc + len / 2;
  4886.         }
  4887.       *set++ = ch;
  4888.     }
  4889.     }
  4890.   *set = '\0';
  4891.   ungetc (ch, f);
  4892.   return alloc;
  4893. }
  4894.  
  4895. /* Read the file name map file for DIRNAME.  */
  4896.  
  4897. static struct file_name_map *
  4898. read_name_map (dirname)
  4899.      char *dirname;
  4900. {
  4901.   /* This structure holds a linked list of file name maps, one per
  4902.      directory.  */
  4903.   struct file_name_map_list
  4904.     {
  4905.       struct file_name_map_list *map_list_next;
  4906.       char *map_list_name;
  4907.       struct file_name_map *map_list_map;
  4908.     };
  4909.   static struct file_name_map_list *map_list;
  4910.   register struct file_name_map_list *map_list_ptr;
  4911.   char *name;
  4912.   FILE *f;
  4913.  
  4914.   for (map_list_ptr = map_list; map_list_ptr;
  4915.        map_list_ptr = map_list_ptr->map_list_next)
  4916.     if (! strcmp (map_list_ptr->map_list_name, dirname))
  4917.       return map_list_ptr->map_list_map;
  4918.  
  4919.   map_list_ptr = ((struct file_name_map_list *)
  4920.           xmalloc (sizeof (struct file_name_map_list)));
  4921.   map_list_ptr->map_list_name = savestring (dirname);
  4922.   map_list_ptr->map_list_map = NULL;
  4923.  
  4924.   name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
  4925.   strcpy (name, dirname);
  4926.   if (*dirname)
  4927.     strcat (name, "/");
  4928.   strcat (name, FILE_NAME_MAP_FILE);
  4929.   f = fopen (name, "r");
  4930.   if (!f)
  4931.     map_list_ptr->map_list_map = NULL;
  4932.   else
  4933.     {
  4934.       int ch;
  4935.       int dirlen = strlen (dirname);
  4936.  
  4937.       while ((ch = getc (f)) != EOF)
  4938.     {
  4939.       char *from, *to;
  4940.       struct file_name_map *ptr;
  4941.  
  4942.       if (is_space[ch])
  4943.         continue;
  4944.       from = read_filename_string (ch, f);
  4945.       while ((ch = getc (f)) != EOF && is_hor_space[ch])
  4946.         ;
  4947.       to = read_filename_string (ch, f);
  4948.  
  4949.       ptr = ((struct file_name_map *)
  4950.          xmalloc (sizeof (struct file_name_map)));
  4951.       ptr->map_from = from;
  4952.  
  4953.       /* Make the real filename absolute.  */
  4954.       if (*to == '/')
  4955.         ptr->map_to = to;
  4956.       else
  4957.         {
  4958.           ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
  4959.           strcpy (ptr->map_to, dirname);
  4960.           ptr->map_to[dirlen] = '/';
  4961.           strcpy (ptr->map_to + dirlen + 1, to);
  4962.           free (to);
  4963.         }          
  4964.  
  4965.       ptr->map_next = map_list_ptr->map_list_map;
  4966.       map_list_ptr->map_list_map = ptr;
  4967.  
  4968.       while ((ch = getc (f)) != '\n')
  4969.         if (ch == EOF)
  4970.           break;
  4971.     }
  4972.       fclose (f);
  4973.     }
  4974.   
  4975.   map_list_ptr->map_list_next = map_list;
  4976.   map_list = map_list_ptr;
  4977.  
  4978.   return map_list_ptr->map_list_map;
  4979. }  
  4980.  
  4981. /* Try to open include file FILENAME.  SEARCHPTR is the directory
  4982.    being tried from the include file search path.  This function maps
  4983.    filenames on file systems based on information read by
  4984.    read_name_map.  */
  4985.  
  4986. static int
  4987. open_include_file (filename, searchptr)
  4988.      char *filename;
  4989.      struct file_name_list *searchptr;
  4990. {
  4991.   register struct file_name_map *map;
  4992.   register char *from;
  4993.   char *p, *dir;
  4994.  
  4995.   if (searchptr && ! searchptr->got_name_map)
  4996.     {
  4997.       searchptr->name_map = read_name_map (searchptr->fname
  4998.                        ? searchptr->fname : ".");
  4999.       searchptr->got_name_map = 1;
  5000.     }
  5001.  
  5002.   /* First check the mapping for the directory we are using.  */
  5003.   if (searchptr && searchptr->name_map)
  5004.     {
  5005.       from = filename;
  5006.       if (searchptr->fname)
  5007.     from += strlen (searchptr->fname) + 1;
  5008.       for (map = searchptr->name_map; map; map = map->map_next)
  5009.     {
  5010.       if (! strcmp (map->map_from, from))
  5011.         {
  5012.           /* Found a match.  */
  5013.           return open (map->map_to, O_RDONLY, 0666);
  5014.         }
  5015.     }
  5016.     }
  5017.  
  5018.   /* Try to find a mapping file for the particular directory we are
  5019.      looking in.  Thus #include <sys/types.h> will look up sys/types.h
  5020.      in /usr/include/header.gcc and look up types.h in
  5021.      /usr/include/sys/header.gcc.  */
  5022.   p = rindex (filename, '/');
  5023. #ifdef DIR_SEPARATOR
  5024.   if (! p) p = rindex (filename, DIR_SEPARATOR);
  5025.   else {
  5026.     char *tmp = rindex (filename, DIR_SEPARATOR);
  5027.     if (tmp != NULL && tmp > p) p = tmp;
  5028.   }
  5029. #endif
  5030.   if (! p)
  5031.     p = filename;
  5032.   if (searchptr
  5033.       && searchptr->fname
  5034.       && strlen (searchptr->fname) == p - filename
  5035.       && ! strncmp (searchptr->fname, filename, p - filename))
  5036.     {
  5037.       /* FILENAME is in SEARCHPTR, which we've already checked.  */
  5038.       return open (filename, O_RDONLY, 0666);
  5039.     }
  5040.  
  5041.   if (p == filename)
  5042.     {
  5043.       dir = ".";
  5044.       from = filename;
  5045.     }
  5046.   else
  5047.     {
  5048.       dir = (char *) alloca (p - filename + 1);
  5049.       bcopy (filename, dir, p - filename);
  5050.       dir[p - filename] = '\0';
  5051.       from = p + 1;
  5052.     }
  5053.   for (map = read_name_map (dir); map; map = map->map_next)
  5054.     if (! strcmp (map->map_from, from))
  5055.       return open (map->map_to, O_RDONLY, 0666);
  5056.  
  5057.   return open (filename, O_RDONLY, 0666);
  5058. }
  5059.  
  5060. /* Process the contents of include file FNAME, already open on descriptor F,
  5061.    with output to OP.
  5062.    SYSTEM_HEADER_P is 1 if this file resides in any one of the known
  5063.    "system" include directories (as decided by the `is_system_include'
  5064.    function above).
  5065.    DIRPTR is the link in the dir path through which this file was found,
  5066.    or 0 if the file name was absolute.  */
  5067.  
  5068. static void
  5069. finclude (f, fname, op, system_header_p, dirptr)
  5070.      int f;
  5071.      char *fname;
  5072.      FILE_BUF *op;
  5073.      int system_header_p;
  5074.      struct file_name_list *dirptr;
  5075. {
  5076.   int st_mode;
  5077.   long st_size;
  5078.   long i;
  5079.   FILE_BUF *fp;            /* For input stack frame */
  5080.   int missing_newline = 0;
  5081.  
  5082.   CHECK_DEPTH (return;);
  5083.  
  5084.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  5085.     {
  5086.       perror_with_name (fname);
  5087.       close (f);
  5088.       return;
  5089.     }
  5090.  
  5091.   fp = &instack[indepth + 1];
  5092.   bzero ((char *) fp, sizeof (FILE_BUF));
  5093.   fp->nominal_fname = fp->fname = fname;
  5094.   fp->length = 0;
  5095.   fp->lineno = 1;
  5096.   fp->if_stack = if_stack;
  5097.   fp->system_header_p = system_header_p;
  5098.   fp->dir = dirptr;
  5099.  
  5100.   if (S_ISREG (st_mode)) {
  5101.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  5102.     fp->bufp = fp->buf;
  5103.  
  5104.     /* Read the file contents, knowing that st_size is an upper bound
  5105.        on the number of bytes we can read.  */
  5106.     fp->length = safe_read (f, (char *) fp->buf, st_size);
  5107.     if (fp->length < 0) goto nope;
  5108.   }
  5109.   else if (S_ISDIR (st_mode)) {
  5110.     error ("directory `%s' specified in #include", fname);
  5111.     close (f);
  5112.     return;
  5113.   } else {
  5114.     /* Cannot count its file size before reading.
  5115.        First read the entire file into heap and
  5116.        copy them into buffer on stack. */
  5117.  
  5118.     int bsize = 2000;
  5119.  
  5120.     st_size = 0;
  5121.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  5122.  
  5123.     for (;;) {
  5124.       i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size);
  5125.       if (i < 0)
  5126.     goto nope;      /* error! */
  5127.       st_size += i;
  5128.       if (st_size != bsize)
  5129.     break;    /* End of file */
  5130.       bsize *= 2;
  5131.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  5132.     }
  5133.     fp->bufp = fp->buf;
  5134.     fp->length = st_size;
  5135.   }
  5136.  
  5137.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  5138.       /* Backslash-newline at end is not good enough.  */
  5139.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  5140.     fp->buf[fp->length++] = '\n';
  5141.     missing_newline = 1;
  5142.   }
  5143.   fp->buf[fp->length] = '\0';
  5144.  
  5145.   /* Close descriptor now, so nesting does not use lots of descriptors.  */
  5146.   close (f);
  5147.  
  5148.   /* Must do this before calling trigraph_pcp, so that the correct file name
  5149.      will be printed in warning messages.  */
  5150.  
  5151.   indepth++;
  5152.   input_file_stack_tick++;
  5153.  
  5154.   if (!no_trigraphs)
  5155.     trigraph_pcp (fp);
  5156.  
  5157.   output_line_directive (fp, op, 0, enter_file);
  5158.   rescan (op, 0);
  5159.  
  5160.   if (missing_newline)
  5161.     fp->lineno--;
  5162.  
  5163.   if (pedantic && missing_newline)
  5164.     pedwarn ("file does not end in newline");
  5165.  
  5166.   indepth--;
  5167.   input_file_stack_tick++;
  5168.   output_line_directive (&instack[indepth], op, 0, leave_file);
  5169.   free (fp->buf);
  5170.   return;
  5171.  
  5172.  nope:
  5173.  
  5174.   perror_with_name (fname);
  5175.   close (f);
  5176.   free (fp->buf);
  5177. }
  5178.  
  5179. /* Record that inclusion of the file named FILE
  5180.    should be controlled by the macro named MACRO_NAME.
  5181.    This means that trying to include the file again
  5182.    will do something if that macro is defined.  */
  5183.  
  5184. static void
  5185. record_control_macro (file, macro_name)
  5186.      char *file;
  5187.      U_CHAR *macro_name;
  5188. {
  5189.   struct file_name_list *new;
  5190.  
  5191.   for (new = all_include_files; new; new = new->next) {
  5192.     if (!strcmp (new->fname, file)) {
  5193.       new->control_macro = macro_name;
  5194.       return;
  5195.     }
  5196.   }
  5197.  
  5198.   /* If the file is not in all_include_files, something's wrong.  */
  5199.   abort ();
  5200. }
  5201.  
  5202. /* Maintain and search list of included files, for #import.  */
  5203.  
  5204. #define IMPORT_HASH_SIZE 31
  5205.  
  5206. struct import_file {
  5207.   char *name;
  5208.   ino_t inode;
  5209.   dev_t dev;
  5210.   struct import_file *next;
  5211. };
  5212.  
  5213. /* Hash table of files already included with #include or #import.  */
  5214.  
  5215. static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
  5216.  
  5217. /* Hash a file name for import_hash_table.  */
  5218.  
  5219. static int 
  5220. import_hash (f)
  5221.      char *f;
  5222. {
  5223.   int val = 0;
  5224.  
  5225.   while (*f) val += *f++;
  5226.   return (val%IMPORT_HASH_SIZE);
  5227. }
  5228.  
  5229. /* Search for file FILENAME in import_hash_table.
  5230.    Return -2 if found, either a matching name or a matching inode.
  5231.    Otherwise, open the file and return a file descriptor if successful
  5232.    or -1 if unsuccessful.  */
  5233.  
  5234. static int
  5235. lookup_import (filename, searchptr)
  5236.      char *filename;
  5237.      struct file_name_list *searchptr;
  5238. {
  5239.   struct import_file *i;
  5240.   int h;
  5241.   int hashval;
  5242.   struct stat sb;
  5243.   int fd;
  5244.  
  5245.   hashval = import_hash (filename);
  5246.  
  5247.   /* Attempt to find file in list of already included files */
  5248.   i = import_hash_table[hashval];
  5249.  
  5250.   while (i) {
  5251.     if (!strcmp (filename, i->name))
  5252.       return -2;        /* return found */
  5253.     i = i->next;
  5254.   }
  5255.   /* Open it and try a match on inode/dev */
  5256.   fd = open_include_file (filename, searchptr);
  5257.   if (fd < 0)
  5258.     return fd;
  5259.   fstat (fd, &sb);
  5260.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  5261.     i = import_hash_table[h];
  5262.     while (i) {
  5263.       /* Compare the inode and the device.
  5264.      Supposedly on some systems the inode is not a scalar.  */
  5265.       if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
  5266.       && i->dev == sb.st_dev) {
  5267.         close (fd);
  5268.         return -2;        /* return found */
  5269.       }
  5270.       i = i->next;
  5271.     }
  5272.   }
  5273.   return fd;            /* Not found, return open file */
  5274. }
  5275.  
  5276. /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
  5277.  
  5278. static void
  5279. add_import (fd, fname)
  5280.      int fd;
  5281.      char *fname;
  5282. {
  5283.   struct import_file *i;
  5284.   int hashval;
  5285.   struct stat sb;
  5286.  
  5287.   hashval = import_hash (fname);
  5288.   fstat (fd, &sb);
  5289.   i = (struct import_file *)xmalloc (sizeof (struct import_file));
  5290.   i->name = xmalloc (strlen (fname)+1);
  5291.   strcpy (i->name, fname);
  5292.   bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
  5293.   i->dev = sb.st_dev;
  5294.   i->next = import_hash_table[hashval];
  5295.   import_hash_table[hashval] = i;
  5296. }
  5297.  
  5298. /* Load the specified precompiled header into core, and verify its
  5299.    preconditions.  PCF indicates the file descriptor to read, which must
  5300.    be a regular file.  FNAME indicates the file name of the original 
  5301.    header.  *LIMIT will be set to an address one past the end of the file.
  5302.    If the preconditions of the file are not satisfied, the buffer is 
  5303.    freed and we return 0.  If the preconditions are satisfied, return
  5304.    the address of the buffer following the preconditions.  The buffer, in
  5305.    this case, should never be freed because various pieces of it will
  5306.    be referred to until all precompiled strings are output at the end of
  5307.    the run.
  5308. */
  5309. static char *
  5310. check_precompiled (pcf, fname, limit)
  5311.      int pcf;
  5312.      char *fname;
  5313.      char **limit;
  5314. {
  5315.   int st_mode;
  5316.   long st_size;
  5317.   int length = 0;
  5318.   char *buf;
  5319.   char *cp;
  5320.  
  5321.   if (pcp_outfile)
  5322.     return 0;
  5323.   
  5324.   if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
  5325.     return 0;
  5326.  
  5327.   if (S_ISREG (st_mode))
  5328.     {
  5329.       buf = xmalloc (st_size + 2);
  5330.       length = safe_read (pcf, buf, st_size);
  5331.       if (length < 0)
  5332.     goto nope;
  5333.     }
  5334.   else
  5335.     abort ();
  5336.     
  5337.   if (length > 0 && buf[length-1] != '\n')
  5338.     buf[length++] = '\n';
  5339.   buf[length] = '\0';
  5340.   
  5341.   *limit = buf + length;
  5342.  
  5343.   /* File is in core.  Check the preconditions. */
  5344.   if (!check_preconditions (buf))
  5345.     goto nope;
  5346.   for (cp = buf; *cp; cp++)
  5347.     ;
  5348. #ifdef DEBUG_PCP
  5349.   fprintf (stderr, "Using preinclude %s\n", fname);
  5350. #endif
  5351.   return cp + 1;
  5352.  
  5353.  nope:
  5354. #ifdef DEBUG_PCP
  5355.   fprintf (stderr, "Cannot use preinclude %s\n", fname);
  5356. #endif
  5357.   free (buf);
  5358.   return 0;
  5359. }
  5360.  
  5361. /* PREC (null terminated) points to the preconditions of a
  5362.    precompiled header.  These are a series of #define and #undef
  5363.    lines which must match the current contents of the hash
  5364.    table.  */
  5365. static int 
  5366. check_preconditions (prec)
  5367.      char *prec;
  5368. {
  5369.   MACRODEF mdef;
  5370.   char *lineend;
  5371.   
  5372.   while (*prec) {
  5373.     lineend = index (prec, '\n');
  5374.     
  5375.     if (*prec++ != '#') {
  5376.       error ("Bad format encountered while reading precompiled file");
  5377.       return 0;
  5378.     }
  5379.     if (!strncmp (prec, "define", 6)) {
  5380.       HASHNODE *hp;
  5381.       
  5382.       prec += 6;
  5383.       mdef = create_definition ((U_CHAR *) prec, (U_CHAR *) lineend, NULL_PTR);
  5384.  
  5385.       if (mdef.defn == 0)
  5386.     abort ();
  5387.       
  5388.       if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
  5389.       || (hp->type != T_MACRO && hp->type != T_CONST)
  5390.       || (hp->type == T_MACRO
  5391.           && !compare_defs (mdef.defn, hp->value.defn)
  5392.           && (mdef.defn->length != 2
  5393.           || mdef.defn->expansion[0] != '\n'
  5394.           || mdef.defn->expansion[1] != ' ')))
  5395.     return 0;
  5396.     } else if (!strncmp (prec, "undef", 5)) {
  5397.       char *name;
  5398.       int len;
  5399.       
  5400.       prec += 5;
  5401.       while (is_hor_space[(U_CHAR) *prec])
  5402.     prec++;
  5403.       name = prec;
  5404.       while (is_idchar[(U_CHAR) *prec])
  5405.     prec++;
  5406.       len = prec - name;
  5407.       
  5408.       if (lookup ((U_CHAR *) name, len, -1))
  5409.     return 0;
  5410.     } else {
  5411.       error ("Bad format encountered while reading precompiled file");
  5412.       return 0;
  5413.     }
  5414.     prec = lineend + 1;
  5415.   }
  5416.   /* They all passed successfully */
  5417.   return 1;
  5418. }
  5419.  
  5420. /* Process the main body of a precompiled file.  BUF points to the
  5421.    string section of the file, following the preconditions.  LIMIT is one
  5422.    character past the end.  NAME is the name of the file being read
  5423.    in.  OP is the main output buffer */
  5424. static void
  5425. pcfinclude (buf, limit, name, op)
  5426.      U_CHAR *buf, *limit, *name;
  5427.      FILE_BUF *op;
  5428. {
  5429.   FILE_BUF tmpbuf;
  5430.   int nstrings;
  5431.   U_CHAR *cp = buf;
  5432.  
  5433.   /* First in the file comes 4 bytes indicating the number of strings, */
  5434.   /* in network byte order. (MSB first).  */
  5435.   nstrings = *cp++;
  5436.   nstrings = (nstrings << 8) | *cp++;
  5437.   nstrings = (nstrings << 8) | *cp++;
  5438.   nstrings = (nstrings << 8) | *cp++;
  5439.   
  5440.   /* Looping over each string... */
  5441.   while (nstrings--) {
  5442.     U_CHAR *string_start;
  5443.     U_CHAR *endofthiskey;
  5444.     STRINGDEF *str;
  5445.     int nkeys;
  5446.     
  5447.     /* Each string starts with a STRINGDEF structure (str), followed */
  5448.     /* by the text of the string (string_start) */
  5449.  
  5450.     /* First skip to a longword boundary */
  5451.     /* ??? Why a 4-byte boundary?  On all machines? */
  5452.     /* NOTE: This works correctly even if HOST_WIDE_INT
  5453.        is narrower than a pointer.
  5454.        Do not try risky measures here to get another type to use!
  5455.        Do not include stddef.h--it will fail!  */
  5456.     if ((HOST_WIDE_INT) cp & 3)
  5457.       cp += 4 - ((HOST_WIDE_INT) cp & 3);
  5458.     
  5459.     /* Now get the string. */
  5460.     str = (STRINGDEF *) (GENERIC_PTR) cp;
  5461.     string_start = cp += sizeof (STRINGDEF);
  5462.     
  5463.     for (; *cp; cp++)        /* skip the string */
  5464.       ;
  5465.     
  5466.     /* We need to macro expand the string here to ensure that the
  5467.        proper definition environment is in place.  If it were only
  5468.        expanded when we find out it is needed, macros necessary for
  5469.        its proper expansion might have had their definitions changed. */
  5470.     tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
  5471.     /* Lineno is already set in the precompiled file */
  5472.     str->contents = tmpbuf.buf;
  5473.     str->len = tmpbuf.length;
  5474.     str->writeflag = 0;
  5475.     str->filename = name;
  5476.     str->output_mark = outbuf.bufp - outbuf.buf;
  5477.     
  5478.     str->chain = 0;
  5479.     *stringlist_tailp = str;
  5480.     stringlist_tailp = &str->chain;
  5481.     
  5482.     /* Next comes a fourbyte number indicating the number of keys */
  5483.     /* for this string. */
  5484.     nkeys = *cp++;
  5485.     nkeys = (nkeys << 8) | *cp++;
  5486.     nkeys = (nkeys << 8) | *cp++;
  5487.     nkeys = (nkeys << 8) | *cp++;
  5488.  
  5489.     /* If this number is -1, then the string is mandatory. */
  5490.     if (nkeys == -1)
  5491.       str->writeflag = 1;
  5492.     else
  5493.       /* Otherwise, for each key, */
  5494.       for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
  5495.     KEYDEF *kp = (KEYDEF *) (GENERIC_PTR) cp;
  5496.     HASHNODE *hp;
  5497.     
  5498.     /* It starts with a KEYDEF structure */
  5499.     cp += sizeof (KEYDEF);
  5500.     
  5501.     /* Find the end of the key.  At the end of this for loop we
  5502.        advance CP to the start of the next key using this variable. */
  5503.     endofthiskey = cp + strlen ((char *) cp);
  5504.     kp->str = str;
  5505.     
  5506.     /* Expand the key, and enter it into the hash table. */
  5507.     tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
  5508.     tmpbuf.bufp = tmpbuf.buf;
  5509.     
  5510.     while (is_hor_space[*tmpbuf.bufp])
  5511.       tmpbuf.bufp++;
  5512.     if (!is_idstart[*tmpbuf.bufp]
  5513.         || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
  5514.       str->writeflag = 1;
  5515.       continue;
  5516.     }
  5517.         
  5518.     hp = lookup (tmpbuf.bufp, -1, -1);
  5519.     if (hp == NULL) {
  5520.       kp->chain = 0;
  5521.       install (tmpbuf.bufp, -1, T_PCSTRING, (char *) kp, -1);
  5522.     }
  5523.     else if (hp->type == T_PCSTRING) {
  5524.       kp->chain = hp->value.keydef;
  5525.       hp->value.keydef = kp;
  5526.     }
  5527.     else
  5528.       str->writeflag = 1;
  5529.       }
  5530.   }
  5531.   /* This output_line_directive serves to switch us back to the current
  5532.      input file in case some of these strings get output (which will 
  5533.      result in line directives for the header file being output). */
  5534.   output_line_directive (&instack[indepth], op, 0, enter_file);
  5535. }
  5536.  
  5537. /* Called from rescan when it hits a key for strings.  Mark them all */
  5538.  /* used and clean up. */
  5539. static void
  5540. pcstring_used (hp)
  5541.      HASHNODE *hp;
  5542. {
  5543.   KEYDEF *kp;
  5544.   
  5545.   for (kp = hp->value.keydef; kp; kp = kp->chain)
  5546.     kp->str->writeflag = 1;
  5547.   delete_macro (hp);
  5548. }
  5549.  
  5550. /* Write the output, interspersing precompiled strings in their */
  5551.  /* appropriate places. */
  5552. static void
  5553. write_output ()
  5554. {
  5555.   STRINGDEF *next_string;
  5556.   U_CHAR *cur_buf_loc;
  5557.   int line_directive_len = 80;
  5558.   char *line_directive = xmalloc (line_directive_len);
  5559.   int len;
  5560.  
  5561.   /* In each run through the loop, either cur_buf_loc == */
  5562.   /* next_string_loc, in which case we print a series of strings, or */
  5563.   /* it is less than next_string_loc, in which case we write some of */
  5564.   /* the buffer. */
  5565.   cur_buf_loc = outbuf.buf; 
  5566.   next_string = stringlist;
  5567.   
  5568.   while (cur_buf_loc < outbuf.bufp || next_string) {
  5569.     if (next_string
  5570.     && cur_buf_loc - outbuf.buf == next_string->output_mark) {
  5571.       if (next_string->writeflag) {
  5572.     len = 4 * strlen ((char *) next_string->filename) + 32;
  5573.     while (len > line_directive_len)
  5574.       line_directive = xrealloc (line_directive, 
  5575.                      line_directive_len *= 2);
  5576.     sprintf (line_directive, "\n# %d ", next_string->lineno);
  5577.     strcpy (quote_string (line_directive + strlen (line_directive),
  5578.                   (char *) next_string->filename),
  5579.         "\n");
  5580.     safe_write (fileno (stdout), line_directive, strlen (line_directive));
  5581.     safe_write (fileno (stdout),
  5582.             (char *) next_string->contents, next_string->len);
  5583.       }          
  5584.       next_string = next_string->chain;
  5585.     }
  5586.     else {
  5587.       len = (next_string
  5588.          ? (next_string->output_mark 
  5589.         - (cur_buf_loc - outbuf.buf))
  5590.          : outbuf.bufp - cur_buf_loc);
  5591.       
  5592.       safe_write (fileno (stdout), (char *) cur_buf_loc, len);
  5593.       cur_buf_loc += len;
  5594.     }
  5595.   }
  5596.   free (line_directive);
  5597. }
  5598.  
  5599. /* Pass a directive through to the output file.
  5600.    BUF points to the contents of the directive, as a contiguous string.
  5601.    LIMIT points to the first character past the end of the directive.
  5602.    KEYWORD is the keyword-table entry for the directive.  */
  5603.  
  5604. static void
  5605. pass_thru_directive (buf, limit, op, keyword)
  5606.      U_CHAR *buf, *limit;
  5607.      FILE_BUF *op;
  5608.      struct directive *keyword;
  5609. {
  5610.   register unsigned keyword_length = keyword->length;
  5611.  
  5612.   check_expand (op, 1 + keyword_length + (limit - buf));
  5613.   *op->bufp++ = '#';
  5614.   bcopy (keyword->name, (char *) op->bufp, keyword_length);
  5615.   op->bufp += keyword_length;
  5616.   if (limit != buf && buf[0] != ' ')
  5617.     *op->bufp++ = ' ';
  5618.   bcopy ((char *) buf, (char *) op->bufp, limit - buf);
  5619.   op->bufp += (limit - buf);
  5620. #if 0
  5621.   *op->bufp++ = '\n';
  5622.   /* Count the line we have just made in the output,
  5623.      to get in sync properly.  */
  5624.   op->lineno++;
  5625. #endif
  5626. }
  5627.  
  5628. /* The arglist structure is built by do_define to tell
  5629.    collect_definition where the argument names begin.  That
  5630.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  5631.    would contain pointers to the strings x, y, and z.
  5632.    Collect_definition would then build a DEFINITION node,
  5633.    with reflist nodes pointing to the places x, y, and z had
  5634.    appeared.  So the arglist is just convenience data passed
  5635.    between these two routines.  It is not kept around after
  5636.    the current #define has been processed and entered into the
  5637.    hash table. */
  5638.  
  5639. struct arglist {
  5640.   struct arglist *next;
  5641.   U_CHAR *name;
  5642.   int length;
  5643.   int argno;
  5644.   char rest_args;
  5645. };
  5646.  
  5647. /* Create a DEFINITION node from a #define directive.  Arguments are 
  5648.    as for do_define. */
  5649. static MACRODEF
  5650. create_definition (buf, limit, op)
  5651.      U_CHAR *buf, *limit;
  5652.      FILE_BUF *op;
  5653. {
  5654.   U_CHAR *bp;            /* temp ptr into input buffer */
  5655.   U_CHAR *symname;        /* remember where symbol name starts */
  5656.   int sym_length;        /* and how long it is */
  5657.   int line = instack[indepth].lineno;
  5658.   char *file = instack[indepth].nominal_fname;
  5659.   int rest_args = 0;
  5660.  
  5661.   DEFINITION *defn;
  5662.   int arglengths = 0;        /* Accumulate lengths of arg names
  5663.                    plus number of args.  */
  5664.   MACRODEF mdef;
  5665.  
  5666.   bp = buf;
  5667.  
  5668.   while (is_hor_space[*bp])
  5669.     bp++;
  5670.  
  5671.   symname = bp;            /* remember where it starts */
  5672.   sym_length = check_macro_name (bp, "macro");
  5673.   bp += sym_length;
  5674.  
  5675.   /* Lossage will occur if identifiers or control keywords are broken
  5676.      across lines using backslash.  This is not the right place to take
  5677.      care of that. */
  5678.  
  5679.   if (*bp == '(') {
  5680.     struct arglist *arg_ptrs = NULL;
  5681.     int argno = 0;
  5682.  
  5683.     bp++;            /* skip '(' */
  5684.     SKIP_WHITE_SPACE (bp);
  5685.  
  5686.     /* Loop over macro argument names.  */
  5687.     while (*bp != ')') {
  5688.       struct arglist *temp;
  5689.  
  5690.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  5691.       temp->name = bp;
  5692.       temp->next = arg_ptrs;
  5693.       temp->argno = argno++;
  5694.       temp->rest_args = 0;
  5695.       arg_ptrs = temp;
  5696.  
  5697.       if (rest_args)
  5698.     pedwarn ("another parameter follows `%s'",
  5699.          rest_extension);
  5700.  
  5701.       if (!is_idstart[*bp])
  5702.     pedwarn ("invalid character in macro parameter name");
  5703.       
  5704.       /* Find the end of the arg name.  */
  5705.       while (is_idchar[*bp]) {
  5706.     bp++;
  5707.     /* do we have a "special" rest-args extension here? */
  5708.     if (limit - bp > REST_EXTENSION_LENGTH &&
  5709.         bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
  5710.       rest_args = 1;
  5711.       temp->rest_args = 1;
  5712.       break;
  5713.     }
  5714.       }
  5715.       temp->length = bp - temp->name;
  5716.       if (rest_args == 1)
  5717.     bp += REST_EXTENSION_LENGTH;
  5718.       arglengths += temp->length + 2;
  5719.       SKIP_WHITE_SPACE (bp);
  5720.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  5721.     error ("badly punctuated parameter list in `#define'");
  5722.     goto nope;
  5723.       }
  5724.       if (*bp == ',') {
  5725.     bp++;
  5726.     SKIP_WHITE_SPACE (bp);
  5727.     /* A comma at this point can only be followed by an identifier.  */
  5728.     if (!is_idstart[*bp]) {
  5729.       error ("badly punctuated parameter list in `#define'");
  5730.       goto nope;
  5731.     }
  5732.       }
  5733.       if (bp >= limit) {
  5734.     error ("unterminated parameter list in `#define'");
  5735.     goto nope;
  5736.       }
  5737.       {
  5738.     struct arglist *otemp;
  5739.  
  5740.     for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
  5741.       if (temp->length == otemp->length &&
  5742.           bcmp (temp->name, otemp->name, temp->length) == 0) {
  5743.           error ("duplicate argument name `%.*s' in `#define'",
  5744.              temp->length, temp->name);
  5745.           goto nope;
  5746.       }
  5747.       }
  5748.     }
  5749.  
  5750.     ++bp;            /* skip paren */
  5751.     SKIP_WHITE_SPACE (bp);
  5752.     /* now everything from bp before limit is the definition. */
  5753.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  5754.     defn->rest_args = rest_args;
  5755.  
  5756.     /* Now set defn->args.argnames to the result of concatenating
  5757.        the argument names in reverse order
  5758.        with comma-space between them.  */
  5759.     defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
  5760.     {
  5761.       struct arglist *temp;
  5762.       int i = 0;
  5763.       for (temp = arg_ptrs; temp; temp = temp->next) {
  5764.     bcopy (temp->name, &defn->args.argnames[i], temp->length);
  5765.     i += temp->length;
  5766.     if (temp->next != 0) {
  5767.       defn->args.argnames[i++] = ',';
  5768.       defn->args.argnames[i++] = ' ';
  5769.     }
  5770.       }
  5771.       defn->args.argnames[i] = 0;
  5772.     }
  5773.   } else {
  5774.     /* Simple expansion or empty definition.  */
  5775.  
  5776.     if (bp < limit)
  5777.       {
  5778.     if (is_hor_space[*bp]) {
  5779.       bp++;
  5780.       SKIP_WHITE_SPACE (bp);
  5781.     } else {
  5782.       switch (*bp) {
  5783.         case '!':  case '"':  case '#':  case '%':  case '&':  case '\'':
  5784.         case ')':  case '*':  case '+':  case ',':  case '-':  case '.':
  5785.         case '/':  case ':':  case ';':  case '<':  case '=':  case '>':
  5786.         case '?':  case '[':  case '\\': case ']':  case '^':  case '{':
  5787.         case '|':  case '}':  case '~':
  5788.           warning ("missing white space after `#define %.*s'",
  5789.                sym_length, symname);
  5790.           break;
  5791.  
  5792.         default:
  5793.           pedwarn ("missing white space after `#define %.*s'",
  5794.                sym_length, symname);
  5795.           break;
  5796.       }
  5797.     }
  5798.       }
  5799.     /* Now everything from bp before limit is the definition. */
  5800.     defn = collect_expansion (bp, limit, -1, NULL_PTR);
  5801.     defn->args.argnames = (U_CHAR *) "";
  5802.   }
  5803.  
  5804.   defn->line = line;
  5805.   defn->file = file;
  5806.  
  5807.   /* OP is null if this is a predefinition */
  5808.   defn->predefined = !op;
  5809.   mdef.defn = defn;
  5810.   mdef.symnam = symname;
  5811.   mdef.symlen = sym_length;
  5812.  
  5813.   return mdef;
  5814.  
  5815.  nope:
  5816.   mdef.defn = 0;
  5817.   return mdef;
  5818. }
  5819.  
  5820. /* Process a #define directive.
  5821. BUF points to the contents of the #define directive, as a contiguous string.
  5822. LIMIT points to the first character past the end of the definition.
  5823. KEYWORD is the keyword-table entry for #define.  */
  5824.  
  5825. static int
  5826. do_define (buf, limit, op, keyword)
  5827.      U_CHAR *buf, *limit;
  5828.      FILE_BUF *op;
  5829.      struct directive *keyword;
  5830. {
  5831.   int hashcode;
  5832.   MACRODEF mdef;
  5833.  
  5834.   /* If this is a precompiler run (with -pcp) pass thru #define directives.  */
  5835.   if (pcp_outfile && op)
  5836.     pass_thru_directive (buf, limit, op, keyword);
  5837.  
  5838.   mdef = create_definition (buf, limit, op);
  5839.   if (mdef.defn == 0)
  5840.     goto nope;
  5841.  
  5842.   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
  5843.  
  5844.   {
  5845.     HASHNODE *hp;
  5846.     if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
  5847.       int ok = 0;
  5848.       /* Redefining a precompiled key is ok.  */
  5849.       if (hp->type == T_PCSTRING)
  5850.     ok = 1;
  5851.       /* Redefining a macro is ok if the definitions are the same.  */
  5852.       else if (hp->type == T_MACRO)
  5853.     ok = ! compare_defs (mdef.defn, hp->value.defn);
  5854.       /* Redefining a constant is ok with -D.  */
  5855.       else if (hp->type == T_CONST)
  5856.         ok = ! done_initializing;
  5857.       /* Print the warning if it's not ok.  */
  5858.       if (!ok) {
  5859.         /* If we are passing through #define and #undef directives, do
  5860.        that for this re-definition now.  */
  5861.         if (debug_output && op)
  5862.       pass_thru_directive (buf, limit, op, keyword);
  5863.  
  5864.     pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
  5865.     if (hp->type == T_MACRO)
  5866.       pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
  5867.                       "this is the location of the previous definition");
  5868.       }
  5869.       /* Replace the old definition.  */
  5870.       hp->type = T_MACRO;
  5871.       hp->value.defn = mdef.defn;
  5872.     } else {
  5873.       /* If we are passing through #define and #undef directives, do
  5874.      that for this new definition now.  */
  5875.       if (debug_output && op)
  5876.     pass_thru_directive (buf, limit, op, keyword);
  5877.       install (mdef.symnam, mdef.symlen, T_MACRO,
  5878.            (char *) mdef.defn, hashcode);
  5879.     }
  5880.   }
  5881.  
  5882.   return 0;
  5883.  
  5884. nope:
  5885.  
  5886.   return 1;
  5887. }
  5888.  
  5889. /* Check a purported macro name SYMNAME, and yield its length.
  5890.    USAGE is the kind of name this is intended for.  */
  5891.  
  5892. static int
  5893. check_macro_name (symname, usage)
  5894.      U_CHAR *symname;
  5895.      char *usage;
  5896. {
  5897.   U_CHAR *p;
  5898.   int sym_length;
  5899.  
  5900.   for (p = symname; is_idchar[*p]; p++)
  5901.     ;
  5902.   sym_length = p - symname;
  5903.   if (sym_length == 0)
  5904.     error ("invalid %s name", usage);
  5905.   else if (!is_idstart[*symname]
  5906.        || (sym_length == 7 && ! bcmp (symname, "defined", 7)))
  5907.     error ("invalid %s name `%.*s'", usage, sym_length, symname);
  5908.   return sym_length;
  5909. }
  5910.  
  5911. /*
  5912.  * return zero if two DEFINITIONs are isomorphic
  5913.  */
  5914. static int
  5915. compare_defs (d1, d2)
  5916.      DEFINITION *d1, *d2;
  5917. {
  5918.   register struct reflist *a1, *a2;
  5919.   register U_CHAR *p1 = d1->expansion;
  5920.   register U_CHAR *p2 = d2->expansion;
  5921.   int first = 1;
  5922.  
  5923.   if (d1->nargs != d2->nargs)
  5924.     return 1;
  5925.   if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
  5926.     return 1;
  5927.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  5928.        a1 = a1->next, a2 = a2->next) {
  5929.     if (!((a1->nchars == a2->nchars && ! bcmp (p1, p2, a1->nchars))
  5930.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  5931.     || a1->argno != a2->argno
  5932.     || a1->stringify != a2->stringify
  5933.     || a1->raw_before != a2->raw_before
  5934.     || a1->raw_after != a2->raw_after)
  5935.       return 1;
  5936.     first = 0;
  5937.     p1 += a1->nchars;
  5938.     p2 += a2->nchars;
  5939.   }
  5940.   if (a1 != a2)
  5941.     return 1;
  5942.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  5943.              p2, d2->length - (p2 - d2->expansion), 1))
  5944.     return 1;
  5945.   return 0;
  5946. }
  5947.  
  5948. /* Return 1 if two parts of two macro definitions are effectively different.
  5949.    One of the parts starts at BEG1 and has LEN1 chars;
  5950.    the other has LEN2 chars at BEG2.
  5951.    Any sequence of whitespace matches any other sequence of whitespace.
  5952.    FIRST means these parts are the first of a macro definition;
  5953.     so ignore leading whitespace entirely.
  5954.    LAST means these parts are the last of a macro definition;
  5955.     so ignore trailing whitespace entirely.  */
  5956.  
  5957. static int
  5958. comp_def_part (first, beg1, len1, beg2, len2, last)
  5959.      int first;
  5960.      U_CHAR *beg1, *beg2;
  5961.      int len1, len2;
  5962.      int last;
  5963. {
  5964.   register U_CHAR *end1 = beg1 + len1;
  5965.   register U_CHAR *end2 = beg2 + len2;
  5966.   if (first) {
  5967.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  5968.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  5969.   }
  5970.   if (last) {
  5971.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  5972.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  5973.   }
  5974.   while (beg1 != end1 && beg2 != end2) {
  5975.     if (is_space[*beg1] && is_space[*beg2]) {
  5976.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  5977.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  5978.     } else if (*beg1 == *beg2) {
  5979.       beg1++; beg2++;
  5980.     } else break;
  5981.   }
  5982.   return (beg1 != end1) || (beg2 != end2);
  5983. }
  5984.  
  5985. /* Read a replacement list for a macro with parameters.
  5986.    Build the DEFINITION structure.
  5987.    Reads characters of text starting at BUF until END.
  5988.    ARGLIST specifies the formal parameters to look for
  5989.    in the text of the definition; NARGS is the number of args
  5990.    in that list, or -1 for a macro name that wants no argument list.
  5991.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  5992.    and NAMELEN is its length in characters.
  5993.    
  5994. Note that comments, backslash-newlines, and leading white space
  5995. have already been deleted from the argument.  */
  5996.  
  5997. /* If there is no trailing whitespace, a Newline Space is added at the end
  5998.    to prevent concatenation that would be contrary to the standard.  */
  5999.  
  6000. static DEFINITION *
  6001. collect_expansion (buf, end, nargs, arglist)
  6002.      U_CHAR *buf, *end;
  6003.      int nargs;
  6004.      struct arglist *arglist;
  6005. {
  6006.   DEFINITION *defn;
  6007.   register U_CHAR *p, *limit, *lastp, *exp_p;
  6008.   struct reflist *endpat = NULL;
  6009.   /* Pointer to first nonspace after last ## seen.  */
  6010.   U_CHAR *concat = 0;
  6011.   /* Pointer to first nonspace after last single-# seen.  */
  6012.   U_CHAR *stringify = 0;
  6013.   /* How those tokens were spelled.  */
  6014.   enum sharp_token_type concat_sharp_token_type = NO_SHARP_TOKEN;
  6015.   enum sharp_token_type stringify_sharp_token_type = NO_SHARP_TOKEN;
  6016.   int maxsize;
  6017.   int expected_delimiter = '\0';
  6018.  
  6019.   /* Scan thru the replacement list, ignoring comments and quoted
  6020.      strings, picking up on the macro calls.  It does a linear search
  6021.      thru the arg list on every potential symbol.  Profiling might say
  6022.      that something smarter should happen. */
  6023.  
  6024.   if (end < buf)
  6025.     abort ();
  6026.  
  6027.   /* Find the beginning of the trailing whitespace.  */
  6028.   limit = end;
  6029.   p = buf;
  6030.   while (p < limit && is_space[limit[-1]]) limit--;
  6031.  
  6032.   /* Allocate space for the text in the macro definition.
  6033.      Each input char may or may not need 1 byte,
  6034.      so this is an upper bound.
  6035.      The extra 3 are for invented trailing newline-marker and final null.  */
  6036.   maxsize = (sizeof (DEFINITION)
  6037.          + (limit - p) + 3);
  6038.   defn = (DEFINITION *) xcalloc (1, maxsize);
  6039.  
  6040.   defn->nargs = nargs;
  6041.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  6042.   lastp = exp_p;
  6043.  
  6044.   if (p[0] == '#'
  6045.       ? p[1] == '#'
  6046.       : p[0] == '%' && p[1] == ':' && p[2] == '%' && p[3] == ':') {
  6047.     error ("`##' at start of macro definition");
  6048.     p += p[0] == '#' ? 2 : 4;
  6049.   }
  6050.  
  6051.   /* Process the main body of the definition.  */
  6052.   while (p < limit) {
  6053.     int skipped_arg = 0;
  6054.     register U_CHAR c = *p++;
  6055.  
  6056.     *exp_p++ = c;
  6057.  
  6058.     if (!traditional) {
  6059.       switch (c) {
  6060.       case '\'':
  6061.       case '\"':
  6062.         if (expected_delimiter != '\0') {
  6063.           if (c == expected_delimiter)
  6064.             expected_delimiter = '\0';
  6065.         } else
  6066.           expected_delimiter = c;
  6067.     break;
  6068.  
  6069.       case '\\':
  6070.     if (p < limit && expected_delimiter) {
  6071.       /* In a string, backslash goes through
  6072.          and makes next char ordinary.  */
  6073.       *exp_p++ = *p++;
  6074.     }
  6075.     break;
  6076.  
  6077.       case '%':
  6078.     if (!expected_delimiter && *p == ':') {
  6079.       /* %: is not a digraph if preceded by an odd number of '<'s.  */
  6080.       U_CHAR *p0 = p - 1;
  6081.       while (buf < p0 && p0[-1] == '<')
  6082.         p0--;
  6083.       if ((p - p0) & 1) {
  6084.         /* Treat %:%: as ## and %: as #.  */
  6085.         if (p[1] == '%' && p[2] == ':') {
  6086.           p += 2;
  6087.           goto sharp_sharp_token;
  6088.         }
  6089.         if (nargs >= 0) {
  6090.           p++;
  6091.           goto sharp_token;
  6092.         }
  6093.       }
  6094.     }
  6095.     break;
  6096.  
  6097.       case '#':
  6098.     /* # is ordinary inside a string.  */
  6099.     if (expected_delimiter)
  6100.       break;
  6101.     if (*p == '#') {
  6102.     sharp_sharp_token:
  6103.       /* ##: concatenate preceding and following tokens.  */
  6104.       /* Take out the first #, discard preceding whitespace.  */
  6105.       exp_p--;
  6106.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  6107.         --exp_p;
  6108.       /* Skip the second #.  */
  6109.       p++;
  6110.       concat_sharp_token_type = c;
  6111.       if (is_hor_space[*p]) {
  6112.         concat_sharp_token_type++;
  6113.         p++;
  6114.         SKIP_WHITE_SPACE (p);
  6115.       }
  6116.       concat = p;
  6117.       if (p == limit)
  6118.         error ("`##' at end of macro definition");
  6119.     } else if (nargs >= 0) {
  6120.       /* Single #: stringify following argument ref.
  6121.          Don't leave the # in the expansion.  */
  6122.     sharp_token:
  6123.       exp_p--;
  6124.       stringify_sharp_token_type = c;
  6125.       if (is_hor_space[*p]) {
  6126.         stringify_sharp_token_type++;
  6127.         p++;
  6128.         SKIP_WHITE_SPACE (p);
  6129.       }
  6130.       if (! is_idstart[*p] || nargs == 0)
  6131.         error ("`#' operator is not followed by a macro argument name");
  6132.       else
  6133.         stringify = p;
  6134.     }
  6135.     break;
  6136.       }
  6137.     } else {
  6138.       /* In -traditional mode, recognize arguments inside strings and
  6139.      and character constants, and ignore special properties of #.
  6140.      Arguments inside strings are considered "stringified", but no
  6141.      extra quote marks are supplied.  */
  6142.       switch (c) {
  6143.       case '\'':
  6144.       case '\"':
  6145.     if (expected_delimiter != '\0') {
  6146.       if (c == expected_delimiter)
  6147.         expected_delimiter = '\0';
  6148.     } else
  6149.       expected_delimiter = c;
  6150.     break;
  6151.  
  6152.       case '\\':
  6153.     /* Backslash quotes delimiters and itself, but not macro args.  */
  6154.     if (expected_delimiter != 0 && p < limit
  6155.         && (*p == expected_delimiter || *p == '\\')) {
  6156.       *exp_p++ = *p++;
  6157.       continue;
  6158.     }
  6159.     break;
  6160.  
  6161.       case '/':
  6162.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  6163.       break;
  6164.     if (*p == '*') {
  6165.       /* If we find a comment that wasn't removed by handle_directive,
  6166.          this must be -traditional.  So replace the comment with
  6167.          nothing at all.  */
  6168.       exp_p--;
  6169.       p += 1;
  6170.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  6171.         p++;
  6172. #if 0
  6173.       /* Mark this as a concatenation-point, as if it had been ##.  */
  6174.       concat = p;
  6175. #endif
  6176.     }
  6177.     break;
  6178.       }
  6179.     }
  6180.  
  6181.     /* Handle the start of a symbol.  */
  6182.     if (is_idchar[c] && nargs > 0) {
  6183.       U_CHAR *id_beg = p - 1;
  6184.       int id_len;
  6185.  
  6186.       --exp_p;
  6187.       while (p != limit && is_idchar[*p]) p++;
  6188.       id_len = p - id_beg;
  6189.  
  6190.       if (is_idstart[c]) {
  6191.     register struct arglist *arg;
  6192.  
  6193.     for (arg = arglist; arg != NULL; arg = arg->next) {
  6194.       struct reflist *tpat;
  6195.  
  6196.       if (arg->name[0] == c
  6197.           && arg->length == id_len
  6198.           && bcmp (arg->name, id_beg, id_len) == 0) {
  6199.         enum sharp_token_type tpat_stringify;
  6200.         if (expected_delimiter) {
  6201.           if (warn_stringify) {
  6202.         if (traditional) {
  6203.           warning ("macro argument `%.*s' is stringified.",
  6204.                id_len, arg->name);
  6205.         } else {
  6206.           warning ("macro arg `%.*s' would be stringified with -traditional.",
  6207.                id_len, arg->name);
  6208.         }
  6209.           }
  6210.           /* If ANSI, don't actually substitute inside a string.  */
  6211.           if (!traditional)
  6212.         break;
  6213.           tpat_stringify = SHARP_TOKEN;
  6214.         } else {
  6215.           tpat_stringify
  6216.         = (stringify == id_beg
  6217.            ? stringify_sharp_token_type : NO_SHARP_TOKEN);
  6218.         }
  6219.         /* make a pat node for this arg and append it to the end of
  6220.            the pat list */
  6221.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  6222.         tpat->next = NULL;
  6223.         tpat->raw_before
  6224.           = concat == id_beg ? concat_sharp_token_type : NO_SHARP_TOKEN;
  6225.         tpat->raw_after = NO_SHARP_TOKEN;
  6226.         tpat->rest_args = arg->rest_args;
  6227.         tpat->stringify = tpat_stringify;
  6228.  
  6229.         if (endpat == NULL)
  6230.           defn->pattern = tpat;
  6231.         else
  6232.           endpat->next = tpat;
  6233.         endpat = tpat;
  6234.  
  6235.         tpat->argno = arg->argno;
  6236.         tpat->nchars = exp_p - lastp;
  6237.         {
  6238.           register U_CHAR *p1 = p;
  6239.           SKIP_WHITE_SPACE (p1);
  6240.           if (p1[0]=='#'
  6241.               ? p1[1]=='#'
  6242.           : p1[0]=='%' && p1[1]==':' && p1[2]=='%' && p1[3]==':')
  6243.         tpat->raw_after = p1[0] + (p != p1);
  6244.         }
  6245.         lastp = exp_p;    /* place to start copying from next time */
  6246.         skipped_arg = 1;
  6247.         break;
  6248.       }
  6249.     }
  6250.       }
  6251.  
  6252.       /* If this was not a macro arg, copy it into the expansion.  */
  6253.       if (! skipped_arg) {
  6254.     register U_CHAR *lim1 = p;
  6255.     p = id_beg;
  6256.     while (p != lim1)
  6257.       *exp_p++ = *p++;
  6258.     if (stringify == id_beg)
  6259.       error ("`#' operator should be followed by a macro argument name");
  6260.       }
  6261.     }
  6262.   }
  6263.  
  6264.   if (!traditional && expected_delimiter == 0) {
  6265.     /* If ANSI, put in a newline-space marker to prevent token pasting.
  6266.        But not if "inside a string" (which in ANSI mode happens only for
  6267.        -D option).  */
  6268.     *exp_p++ = '\n';
  6269.     *exp_p++ = ' ';
  6270.   }
  6271.  
  6272.   *exp_p = '\0';
  6273.  
  6274.   defn->length = exp_p - defn->expansion;
  6275.  
  6276.   /* Crash now if we overrun the allocated size.  */
  6277.   if (defn->length + 1 > maxsize)
  6278.     abort ();
  6279.  
  6280. #if 0
  6281. /* This isn't worth the time it takes.  */
  6282.   /* give back excess storage */
  6283.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  6284. #endif
  6285.  
  6286.   return defn;
  6287. }
  6288.  
  6289. static int
  6290. do_assert (buf, limit, op, keyword)
  6291.      U_CHAR *buf, *limit;
  6292.      FILE_BUF *op;
  6293.      struct directive *keyword;
  6294. {
  6295.   U_CHAR *bp;            /* temp ptr into input buffer */
  6296.   U_CHAR *symname;        /* remember where symbol name starts */
  6297.   int sym_length;        /* and how long it is */
  6298.   struct arglist *tokens = NULL;
  6299.  
  6300.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  6301.     pedwarn ("ANSI C does not allow `#assert'");
  6302.  
  6303.   bp = buf;
  6304.  
  6305.   while (is_hor_space[*bp])
  6306.     bp++;
  6307.  
  6308.   symname = bp;            /* remember where it starts */
  6309.   sym_length = check_macro_name (bp, "assertion");
  6310.   bp += sym_length;
  6311.   /* #define doesn't do this, but we should.  */
  6312.   SKIP_WHITE_SPACE (bp);
  6313.  
  6314.   /* Lossage will occur if identifiers or control tokens are broken
  6315.      across lines using backslash.  This is not the right place to take
  6316.      care of that. */
  6317.  
  6318.   if (*bp != '(') {
  6319.     error ("missing token-sequence in `#assert'");
  6320.     return 1;
  6321.   }
  6322.  
  6323.   {
  6324.     int error_flag = 0;
  6325.  
  6326.     bp++;            /* skip '(' */
  6327.     SKIP_WHITE_SPACE (bp);
  6328.  
  6329.     tokens = read_token_list (&bp, limit, &error_flag);
  6330.     if (error_flag)
  6331.       return 1;
  6332.     if (tokens == 0) {
  6333.       error ("empty token-sequence in `#assert'");
  6334.       return 1;
  6335.     }
  6336.  
  6337.     ++bp;            /* skip paren */
  6338.     SKIP_WHITE_SPACE (bp);
  6339.   }
  6340.  
  6341.   /* If this name isn't already an assertion name, make it one.
  6342.      Error if it was already in use in some other way.  */
  6343.  
  6344.   {
  6345.     ASSERTION_HASHNODE *hp;
  6346.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  6347.     struct tokenlist_list *value
  6348.       = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
  6349.  
  6350.     hp = assertion_lookup (symname, sym_length, hashcode);
  6351.     if (hp == NULL) {
  6352.       if (sym_length == 7 && ! bcmp (symname, "defined", 7))
  6353.     error ("`defined' redefined as assertion");
  6354.       hp = assertion_install (symname, sym_length, hashcode);
  6355.     }
  6356.  
  6357.     /* Add the spec'd token-sequence to the list of such.  */
  6358.     value->tokens = tokens;
  6359.     value->next = hp->value;
  6360.     hp->value = value;
  6361.   }
  6362.  
  6363.   return 0;
  6364. }
  6365.  
  6366. static int
  6367. do_unassert (buf, limit, op, keyword)
  6368.      U_CHAR *buf, *limit;
  6369.      FILE_BUF *op;
  6370.      struct directive *keyword;
  6371. {
  6372.   U_CHAR *bp;            /* temp ptr into input buffer */
  6373.   U_CHAR *symname;        /* remember where symbol name starts */
  6374.   int sym_length;        /* and how long it is */
  6375.  
  6376.   struct arglist *tokens = NULL;
  6377.   int tokens_specified = 0;
  6378.  
  6379.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  6380.     pedwarn ("ANSI C does not allow `#unassert'");
  6381.  
  6382.   bp = buf;
  6383.  
  6384.   while (is_hor_space[*bp])
  6385.     bp++;
  6386.  
  6387.   symname = bp;            /* remember where it starts */
  6388.   sym_length = check_macro_name (bp, "assertion");
  6389.   bp += sym_length;
  6390.   /* #define doesn't do this, but we should.  */
  6391.   SKIP_WHITE_SPACE (bp);
  6392.  
  6393.   /* Lossage will occur if identifiers or control tokens are broken
  6394.      across lines using backslash.  This is not the right place to take
  6395.      care of that. */
  6396.  
  6397.   if (*bp == '(') {
  6398.     int error_flag = 0;
  6399.  
  6400.     bp++;            /* skip '(' */
  6401.     SKIP_WHITE_SPACE (bp);
  6402.  
  6403.     tokens = read_token_list (&bp, limit, &error_flag);
  6404.     if (error_flag)
  6405.       return 1;
  6406.     if (tokens == 0) {
  6407.       error ("empty token list in `#unassert'");
  6408.       return 1;
  6409.     }
  6410.  
  6411.     tokens_specified = 1;
  6412.  
  6413.     ++bp;            /* skip paren */
  6414.     SKIP_WHITE_SPACE (bp);
  6415.   }
  6416.  
  6417.   {
  6418.     ASSERTION_HASHNODE *hp;
  6419.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  6420.     struct tokenlist_list *tail, *prev;
  6421.  
  6422.     hp = assertion_lookup (symname, sym_length, hashcode);
  6423.     if (hp == NULL)
  6424.       return 1;
  6425.  
  6426.     /* If no token list was specified, then eliminate this assertion
  6427.        entirely.  */
  6428.     if (! tokens_specified) {
  6429.       struct tokenlist_list *next;
  6430.       for (tail = hp->value; tail; tail = next) {
  6431.     next = tail->next;
  6432.     free_token_list (tail->tokens);
  6433.     free (tail);
  6434.       }
  6435.       delete_assertion (hp);
  6436.     } else {
  6437.       /* If a list of tokens was given, then delete any matching list.  */
  6438.  
  6439.       tail = hp->value;
  6440.       prev = 0;
  6441.       while (tail) {
  6442.     struct tokenlist_list *next = tail->next;
  6443.     if (compare_token_lists (tail->tokens, tokens)) {
  6444.       if (prev)
  6445.         prev->next = next;
  6446.       else
  6447.         hp->value = tail->next;
  6448.       free_token_list (tail->tokens);
  6449.       free (tail);
  6450.     } else {
  6451.       prev = tail;
  6452.     }
  6453.     tail = next;
  6454.       }
  6455.     }
  6456.   }
  6457.  
  6458.   return 0;
  6459. }
  6460.  
  6461. /* Test whether there is an assertion named NAME
  6462.    and optionally whether it has an asserted token list TOKENS.
  6463.    NAME is not null terminated; its length is SYM_LENGTH.
  6464.    If TOKENS_SPECIFIED is 0, then don't check for any token list.  */
  6465.  
  6466. int
  6467. check_assertion (name, sym_length, tokens_specified, tokens)
  6468.      U_CHAR *name;
  6469.      int sym_length;
  6470.      int tokens_specified;
  6471.      struct arglist *tokens;
  6472. {
  6473.   ASSERTION_HASHNODE *hp;
  6474.   int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
  6475.  
  6476.   if (pedantic && !instack[indepth].system_header_p)
  6477.     pedwarn ("ANSI C does not allow testing assertions");
  6478.  
  6479.   hp = assertion_lookup (name, sym_length, hashcode);
  6480.   if (hp == NULL)
  6481.     /* It is not an assertion; just return false.  */
  6482.     return 0;
  6483.  
  6484.   /* If no token list was specified, then value is 1.  */
  6485.   if (! tokens_specified)
  6486.     return 1;
  6487.  
  6488.   {
  6489.     struct tokenlist_list *tail;
  6490.  
  6491.     tail = hp->value;
  6492.  
  6493.     /* If a list of tokens was given,
  6494.        then succeed if the assertion records a matching list.  */
  6495.  
  6496.     while (tail) {
  6497.       if (compare_token_lists (tail->tokens, tokens))
  6498.     return 1;
  6499.       tail = tail->next;
  6500.     }
  6501.  
  6502.     /* Fail if the assertion has no matching list.  */
  6503.     return 0;
  6504.   }
  6505. }
  6506.  
  6507. /* Compare two lists of tokens for equality including order of tokens.  */
  6508.  
  6509. static int
  6510. compare_token_lists (l1, l2)
  6511.      struct arglist *l1, *l2;
  6512. {
  6513.   while (l1 && l2) {
  6514.     if (l1->length != l2->length)
  6515.       return 0;
  6516.     if (bcmp (l1->name, l2->name, l1->length))
  6517.       return 0;
  6518.     l1 = l1->next;
  6519.     l2 = l2->next;
  6520.   }
  6521.  
  6522.   /* Succeed if both lists end at the same time.  */
  6523.   return l1 == l2;
  6524. }
  6525.  
  6526. /* Read a space-separated list of tokens ending in a close parenthesis.
  6527.    Return a list of strings, in the order they were written.
  6528.    (In case of error, return 0 and store -1 in *ERROR_FLAG.)
  6529.    Parse the text starting at *BPP, and update *BPP.
  6530.    Don't parse beyond LIMIT.  */
  6531.  
  6532. static struct arglist *
  6533. read_token_list (bpp, limit, error_flag)
  6534.      U_CHAR **bpp;
  6535.      U_CHAR *limit;
  6536.      int *error_flag;
  6537. {
  6538.   struct arglist *token_ptrs = 0;
  6539.   U_CHAR *bp = *bpp;
  6540.   int depth = 1;
  6541.  
  6542.   *error_flag = 0;
  6543.  
  6544.   /* Loop over the assertion value tokens.  */
  6545.   while (depth > 0) {
  6546.     struct arglist *temp;
  6547.     int eofp = 0;
  6548.     U_CHAR *beg = bp;
  6549.  
  6550.     /* Find the end of the token.  */
  6551.     if (*bp == '(') {
  6552.       bp++;
  6553.       depth++;
  6554.     } else if (*bp == ')') {
  6555.       depth--;
  6556.       if (depth == 0)
  6557.     break;
  6558.       bp++;
  6559.     } else if (*bp == '"' || *bp == '\'')
  6560.       bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  6561.     else
  6562.       while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
  6563.          && *bp != '"' && *bp != '\'' && bp != limit)
  6564.     bp++;
  6565.  
  6566.     temp = (struct arglist *) xmalloc (sizeof (struct arglist));
  6567.     temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
  6568.     bcopy ((char *) beg, (char *) temp->name, bp - beg);
  6569.     temp->name[bp - beg] = 0;
  6570.     temp->next = token_ptrs;
  6571.     token_ptrs = temp;
  6572.     temp->length = bp - beg;
  6573.  
  6574.     SKIP_WHITE_SPACE (bp);
  6575.  
  6576.     if (bp >= limit) {
  6577.       error ("unterminated token sequence in `#assert' or `#unassert'");
  6578.       *error_flag = -1;
  6579.       return 0;
  6580.     }
  6581.   }
  6582.   *bpp = bp;
  6583.  
  6584.   /* We accumulated the names in reverse order.
  6585.      Now reverse them to get the proper order.  */
  6586.   {
  6587.     register struct arglist *prev = 0, *this, *next;
  6588.     for (this = token_ptrs; this; this = next) {
  6589.       next = this->next;
  6590.       this->next = prev;
  6591.       prev = this;
  6592.     }
  6593.     return prev;
  6594.   }
  6595. }
  6596.  
  6597. static void
  6598. free_token_list (tokens)
  6599.      struct arglist *tokens;
  6600. {
  6601.   while (tokens) {
  6602.     struct arglist *next = tokens->next;
  6603.     free (tokens->name);
  6604.     free (tokens);
  6605.     tokens = next;
  6606.   }
  6607. }
  6608.  
  6609. /*
  6610.  * Install a name in the assertion hash table.
  6611.  *
  6612.  * If LEN is >= 0, it is the length of the name.
  6613.  * Otherwise, compute the length by scanning the entire name.
  6614.  *
  6615.  * If HASH is >= 0, it is the precomputed hash code.
  6616.  * Otherwise, compute the hash code.
  6617.  */
  6618. static ASSERTION_HASHNODE *
  6619. assertion_install (name, len, hash)
  6620.      U_CHAR *name;
  6621.      int len;
  6622.      int hash;
  6623. {
  6624.   register ASSERTION_HASHNODE *hp;
  6625.   register int i, bucket;
  6626.   register U_CHAR *p, *q;
  6627.  
  6628.   i = sizeof (ASSERTION_HASHNODE) + len + 1;
  6629.   hp = (ASSERTION_HASHNODE *) xmalloc (i);
  6630.   bucket = hash;
  6631.   hp->bucket_hdr = &assertion_hashtab[bucket];
  6632.   hp->next = assertion_hashtab[bucket];
  6633.   assertion_hashtab[bucket] = hp;
  6634.   hp->prev = NULL;
  6635.   if (hp->next != NULL)
  6636.     hp->next->prev = hp;
  6637.   hp->length = len;
  6638.   hp->value = 0;
  6639.   hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
  6640.   p = hp->name;
  6641.   q = name;
  6642.   for (i = 0; i < len; i++)
  6643.     *p++ = *q++;
  6644.   hp->name[len] = 0;
  6645.   return hp;
  6646. }
  6647.  
  6648. /*
  6649.  * find the most recent hash node for name name (ending with first
  6650.  * non-identifier char) installed by install
  6651.  *
  6652.  * If LEN is >= 0, it is the length of the name.
  6653.  * Otherwise, compute the length by scanning the entire name.
  6654.  *
  6655.  * If HASH is >= 0, it is the precomputed hash code.
  6656.  * Otherwise, compute the hash code.
  6657.  */
  6658. static ASSERTION_HASHNODE *
  6659. assertion_lookup (name, len, hash)
  6660.      U_CHAR *name;
  6661.      int len;
  6662.      int hash;
  6663. {
  6664.   register ASSERTION_HASHNODE *bucket;
  6665.  
  6666.   bucket = assertion_hashtab[hash];
  6667.   while (bucket) {
  6668.     if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
  6669.       return bucket;
  6670.     bucket = bucket->next;
  6671.   }
  6672.   return NULL;
  6673. }
  6674.  
  6675. static void
  6676. delete_assertion (hp)
  6677.      ASSERTION_HASHNODE *hp;
  6678. {
  6679.  
  6680.   if (hp->prev != NULL)
  6681.     hp->prev->next = hp->next;
  6682.   if (hp->next != NULL)
  6683.     hp->next->prev = hp->prev;
  6684.  
  6685.   /* make sure that the bucket chain header that
  6686.      the deleted guy was on points to the right thing afterwards. */
  6687.   if (hp == *hp->bucket_hdr)
  6688.     *hp->bucket_hdr = hp->next;
  6689.  
  6690.   free (hp);
  6691. }
  6692.  
  6693. /*
  6694.  * interpret #line directive.  Remembers previously seen fnames
  6695.  * in its very own hash table.
  6696.  */
  6697. #define FNAME_HASHSIZE 37
  6698.  
  6699. static int
  6700. do_line (buf, limit, op, keyword)
  6701.      U_CHAR *buf, *limit;
  6702.      FILE_BUF *op;
  6703.      struct directive *keyword;
  6704. {
  6705.   register U_CHAR *bp;
  6706.   FILE_BUF *ip = &instack[indepth];
  6707.   FILE_BUF tem;
  6708.   int new_lineno;
  6709.   enum file_change_code file_change = same_file;
  6710.  
  6711.   /* Expand any macros.  */
  6712.   tem = expand_to_temp_buffer (buf, limit, 0, 0);
  6713.  
  6714.   /* Point to macroexpanded line, which is null-terminated now.  */
  6715.   bp = tem.buf;
  6716.   SKIP_WHITE_SPACE (bp);
  6717.  
  6718.   if (!isdigit (*bp)) {
  6719.     error ("invalid format `#line' directive");
  6720.     return 0;
  6721.   }
  6722.  
  6723.   /* The Newline at the end of this line remains to be processed.
  6724.      To put the next line at the specified line number,
  6725.      we must store a line number now that is one less.  */
  6726.   new_lineno = atoi ((char *) bp) - 1;
  6727.  
  6728.   /* NEW_LINENO is one less than the actual line number here.  */
  6729.   if (pedantic && new_lineno < 0)
  6730.     pedwarn ("line number out of range in `#line' directive");
  6731.  
  6732.   /* skip over the line number.  */
  6733.   while (isdigit (*bp))
  6734.     bp++;
  6735.  
  6736. #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
  6737.   if (*bp && !is_space[*bp]) {
  6738.     error ("invalid format `#line' directive");
  6739.     return;
  6740.   }
  6741. #endif
  6742.  
  6743.   SKIP_WHITE_SPACE (bp);
  6744.  
  6745.   if (*bp == '\"') {
  6746.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  6747.     HASHNODE *hp, **hash_bucket;
  6748.     U_CHAR *fname, *p;
  6749.     int fname_length;
  6750.  
  6751.     fname = ++bp;
  6752.  
  6753.     /* Turn the file name, which is a character string literal,
  6754.        into a null-terminated string.  Do this in place.  */
  6755.     p = bp;
  6756.     for (;;)
  6757.       switch ((*p++ = *bp++)) {
  6758.       case '\0':
  6759.     error ("invalid format `#line' directive");
  6760.     return 0;
  6761.  
  6762.       case '\\':
  6763.     {
  6764.       char *bpc = (char *) bp;
  6765.       int c = parse_escape (&bpc);
  6766.       bp = (U_CHAR *) bpc;
  6767.       if (c < 0)
  6768.         p--;
  6769.       else
  6770.         p[-1] = c;
  6771.     }
  6772.     break;
  6773.  
  6774.       case '\"':
  6775.     p[-1] = 0;
  6776.     goto fname_done;
  6777.       }
  6778.   fname_done:
  6779.     fname_length = p - fname;
  6780.  
  6781.     SKIP_WHITE_SPACE (bp);
  6782.     if (*bp) {
  6783.       if (pedantic)
  6784.     pedwarn ("garbage at end of `#line' directive");
  6785.       if (*bp == '1')
  6786.     file_change = enter_file;
  6787.       else if (*bp == '2')
  6788.     file_change = leave_file;
  6789.       else if (*bp == '3')
  6790.     ip->system_header_p = 1;
  6791.       else if (*bp == '4')
  6792.     ip->system_header_p = 2;
  6793.       else {
  6794.     error ("invalid format `#line' directive");
  6795.     return 0;
  6796.       }
  6797.  
  6798.       bp++;
  6799.       SKIP_WHITE_SPACE (bp);
  6800.       if (*bp == '3') {
  6801.     ip->system_header_p = 1;
  6802.     bp++;
  6803.     SKIP_WHITE_SPACE (bp);
  6804.       }
  6805.       if (*bp == '4') {
  6806.     ip->system_header_p = 2;
  6807.     bp++;
  6808.     SKIP_WHITE_SPACE (bp);
  6809.       }
  6810.       if (*bp) {
  6811.     error ("invalid format `#line' directive");
  6812.     return 0;
  6813.       }
  6814.     }
  6815.  
  6816.     hash_bucket =
  6817.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  6818.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  6819.       if (hp->length == fname_length &&
  6820.       bcmp (hp->value.cpval, fname, fname_length) == 0) {
  6821.     ip->nominal_fname = hp->value.cpval;
  6822.     break;
  6823.       }
  6824.     if (hp == 0) {
  6825.       /* Didn't find it; cons up a new one.  */
  6826.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  6827.       hp->next = *hash_bucket;
  6828.       *hash_bucket = hp;
  6829.  
  6830.       hp->length = fname_length;
  6831.       ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  6832.       bcopy (fname, hp->value.cpval, fname_length);
  6833.     }
  6834.   } else if (*bp) {
  6835.     error ("invalid format `#line' directive");
  6836.     return 0;
  6837.   }
  6838.  
  6839.   ip->lineno = new_lineno;
  6840.   output_line_directive (ip, op, 0, file_change);
  6841.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  6842.   return 0;
  6843. }
  6844.  
  6845. /*
  6846.  * remove the definition of a symbol from the symbol table.
  6847.  * according to un*x /lib/cpp, it is not an error to undef
  6848.  * something that has no definitions, so it isn't one here either.
  6849.  */
  6850.  
  6851. static int
  6852. do_undef (buf, limit, op, keyword)
  6853.      U_CHAR *buf, *limit;
  6854.      FILE_BUF *op;
  6855.      struct directive *keyword;
  6856. {
  6857.   int sym_length;
  6858.   HASHNODE *hp;
  6859.   U_CHAR *orig_buf = buf;
  6860.  
  6861.   /* If this is a precompiler run (with -pcp) pass thru #undef directives.  */
  6862.   if (pcp_outfile && op)
  6863.     pass_thru_directive (buf, limit, op, keyword);
  6864.  
  6865.   SKIP_WHITE_SPACE (buf);
  6866.   sym_length = check_macro_name (buf, "macro");
  6867.  
  6868.   while ((hp = lookup (buf, sym_length, -1)) != NULL) {
  6869.     /* If we are generating additional info for debugging (with -g) we
  6870.        need to pass through all effective #undef directives.  */
  6871.     if (debug_output && op)
  6872.       pass_thru_directive (orig_buf, limit, op, keyword);
  6873.     if (hp->type != T_MACRO)
  6874.       warning ("undefining `%s'", hp->name);
  6875.     delete_macro (hp);
  6876.   }
  6877.  
  6878.   if (pedantic) {
  6879.     buf += sym_length;
  6880.     SKIP_WHITE_SPACE (buf);
  6881.     if (buf != limit)
  6882.       pedwarn ("garbage after `#undef' directive");
  6883.   }
  6884.   return 0;
  6885. }
  6886.  
  6887. /*
  6888.  * Report an error detected by the program we are processing.
  6889.  * Use the text of the line in the error message.
  6890.  * (We use error because it prints the filename & line#.)
  6891.  */
  6892.  
  6893. static int
  6894. do_error (buf, limit, op, keyword)
  6895.      U_CHAR *buf, *limit;
  6896.      FILE_BUF *op;
  6897.      struct directive *keyword;
  6898. {
  6899.   int length = limit - buf;
  6900.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  6901.   bcopy ((char *) buf, (char *) copy, length);
  6902.   copy[length] = 0;
  6903.   SKIP_WHITE_SPACE (copy);
  6904.   error ("#error %s", copy);
  6905.   return 0;
  6906. }
  6907.  
  6908. /*
  6909.  * Report a warning detected by the program we are processing.
  6910.  * Use the text of the line in the warning message, then continue.
  6911.  * (We use error because it prints the filename & line#.)
  6912.  */
  6913.  
  6914. static int
  6915. do_warning (buf, limit, op, keyword)
  6916.      U_CHAR *buf, *limit;
  6917.      FILE_BUF *op;
  6918.      struct directive *keyword;
  6919. {
  6920.   int length = limit - buf;
  6921.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  6922.   bcopy ((char *) buf, (char *) copy, length);
  6923.   copy[length] = 0;
  6924.   SKIP_WHITE_SPACE (copy);
  6925.   warning ("#warning %s", copy);
  6926.   return 0;
  6927. }
  6928.  
  6929. /* Remember the name of the current file being read from so that we can
  6930.    avoid ever including it again.  */
  6931.  
  6932. static void
  6933. do_once ()
  6934. {
  6935.   int i;
  6936.   FILE_BUF *ip = NULL;
  6937.  
  6938.   for (i = indepth; i >= 0; i--)
  6939.     if (instack[i].fname != NULL) {
  6940.       ip = &instack[i];
  6941.       break;
  6942.     }
  6943.  
  6944.   if (ip != NULL) {
  6945.     struct file_name_list *new;
  6946.     
  6947.     new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  6948.     new->next = dont_repeat_files;
  6949.     dont_repeat_files = new;
  6950.     new->fname = savestring (ip->fname);
  6951.     new->control_macro = 0;
  6952.     new->got_name_map = 0;
  6953.     new->c_system_include_path = 0;
  6954.   }
  6955. }
  6956.  
  6957. /* #ident has already been copied to the output file, so just ignore it.  */
  6958.  
  6959. static int
  6960. do_ident (buf, limit, op, keyword)
  6961.      U_CHAR *buf, *limit;
  6962.      FILE_BUF *op;
  6963.      struct directive *keyword;
  6964. {
  6965.   FILE_BUF trybuf;
  6966.   int len;
  6967.  
  6968.   /* Allow #ident in system headers, since that's not user's fault.  */
  6969.   if (pedantic && !instack[indepth].system_header_p)
  6970.     pedwarn ("ANSI C does not allow `#ident'");
  6971.  
  6972.   trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  6973.   buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  6974.   bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  6975.   limit = buf + (trybuf.bufp - trybuf.buf);
  6976.   len = (limit - buf);
  6977.   free (trybuf.buf);
  6978.  
  6979.   /* Output directive name.  */
  6980.   check_expand (op, 7);
  6981.   bcopy ("#ident ", (char *) op->bufp, 7);
  6982.   op->bufp += 7;
  6983.  
  6984.   /* Output the expanded argument line.  */
  6985.   check_expand (op, len);
  6986.   bcopy ((char *) buf, (char *) op->bufp, len);
  6987.   op->bufp += len;
  6988.  
  6989.   return 0;
  6990. }
  6991.  
  6992. /* #pragma and its argument line have already been copied to the output file.
  6993.    Just check for some recognized pragmas that need validation here.  */
  6994.  
  6995. static int
  6996. do_pragma (buf, limit, op, keyword)
  6997.      U_CHAR *buf, *limit;
  6998.      FILE_BUF *op;
  6999.      struct directive *keyword;
  7000. {
  7001.   SKIP_WHITE_SPACE (buf);
  7002.   if (!strncmp ((char *) buf, "once", 4)) {
  7003.     /* Allow #pragma once in system headers, since that's not the user's
  7004.        fault.  */
  7005.     if (!instack[indepth].system_header_p)
  7006.       warning ("`#pragma once' is obsolete");
  7007.     do_once ();
  7008.   }
  7009.  
  7010.   if (!strncmp ((char *) buf, "implementation", 14)) {
  7011.     /* Be quiet about `#pragma implementation' for a file only if it hasn't
  7012.        been included yet.  */
  7013.     struct file_name_list *ptr;
  7014.     U_CHAR *p = buf + 14, *fname, *inc_fname;
  7015.     SKIP_WHITE_SPACE (p);
  7016.     if (*p == '\n' || *p != '\"')
  7017.       return 0;
  7018.  
  7019.     fname = p + 1;
  7020.     if ((p = (U_CHAR *) index ((char *) fname, '\"')))
  7021.       *p = '\0';
  7022.     
  7023.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  7024.       inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
  7025.       inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
  7026.       if (inc_fname && !strcmp ((char *) inc_fname, (char *) fname))
  7027.     warning ("`#pragma implementation' for `%s' appears after file is included",
  7028.          fname);
  7029.     }
  7030.   }
  7031.  
  7032.   return 0;
  7033. }
  7034.  
  7035. #if 0
  7036. /* This was a fun hack, but #pragma seems to start to be useful.
  7037.    By failing to recognize it, we pass it through unchanged to cc1.  */
  7038.  
  7039. /*
  7040.  * the behavior of the #pragma directive is implementation defined.
  7041.  * this implementation defines it as follows.
  7042.  */
  7043.  
  7044. static int
  7045. do_pragma ()
  7046. {
  7047.   close (0);
  7048.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  7049.     goto nope;
  7050.   close (1);
  7051.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  7052.     goto nope;
  7053.   execl ("/usr/games/hack", "#pragma", 0);
  7054.   execl ("/usr/games/rogue", "#pragma", 0);
  7055.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  7056.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  7057. nope:
  7058.   fatal ("You are in a maze of twisty compiler features, all different");
  7059. }
  7060. #endif
  7061.  
  7062. #ifdef SCCS_DIRECTIVE
  7063.  
  7064. /* Just ignore #sccs, on systems where we define it at all.  */
  7065.  
  7066. static int
  7067. do_sccs (buf, limit, op, keyword)
  7068.      U_CHAR *buf, *limit;
  7069.      FILE_BUF *op;
  7070.      struct directive *keyword;
  7071. {
  7072.   if (pedantic)
  7073.     pedwarn ("ANSI C does not allow `#sccs'");
  7074.   return 0;
  7075. }
  7076.  
  7077. #endif /* defined (SCCS_DIRECTIVE) */
  7078.  
  7079. /*
  7080.  * handle #if directive by
  7081.  *   1) inserting special `defined' keyword into the hash table
  7082.  *    that gets turned into 0 or 1 by special_symbol (thus,
  7083.  *    if the luser has a symbol called `defined' already, it won't
  7084.  *      work inside the #if directive)
  7085.  *   2) rescan the input into a temporary output buffer
  7086.  *   3) pass the output buffer to the yacc parser and collect a value
  7087.  *   4) clean up the mess left from steps 1 and 2.
  7088.  *   5) call conditional_skip to skip til the next #endif (etc.),
  7089.  *      or not, depending on the value from step 3.
  7090.  */
  7091.  
  7092. static int
  7093. do_if (buf, limit, op, keyword)
  7094.      U_CHAR *buf, *limit;
  7095.      FILE_BUF *op;
  7096.      struct directive *keyword;
  7097. {
  7098.   HOST_WIDE_INT value;
  7099.   FILE_BUF *ip = &instack[indepth];
  7100.  
  7101.   value = eval_if_expression (buf, limit - buf);
  7102.   conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
  7103.   return 0;
  7104. }
  7105.  
  7106. /*
  7107.  * handle a #elif directive by not changing  if_stack  either.
  7108.  * see the comment above do_else.
  7109.  */
  7110.  
  7111. static int
  7112. do_elif (buf, limit, op, keyword)
  7113.      U_CHAR *buf, *limit;
  7114.      FILE_BUF *op;
  7115.      struct directive *keyword;
  7116. {
  7117.   HOST_WIDE_INT value;
  7118.   FILE_BUF *ip = &instack[indepth];
  7119.  
  7120.   if (if_stack == instack[indepth].if_stack) {
  7121.     error ("`#elif' not within a conditional");
  7122.     return 0;
  7123.   } else {
  7124.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  7125.       error ("`#elif' after `#else'");
  7126.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  7127.       if (if_stack->fname != NULL && ip->fname != NULL &&
  7128.       strcmp (if_stack->fname, ip->nominal_fname) != 0)
  7129.     fprintf (stderr, ", file %s", if_stack->fname);
  7130.       fprintf (stderr, ")\n");
  7131.     }
  7132.     if_stack->type = T_ELIF;
  7133.   }
  7134.  
  7135.   if (if_stack->if_succeeded)
  7136.     skip_if_group (ip, 0, op);
  7137.   else {
  7138.     value = eval_if_expression (buf, limit - buf);
  7139.     if (value == 0)
  7140.       skip_if_group (ip, 0, op);
  7141.     else {
  7142.       ++if_stack->if_succeeded;    /* continue processing input */
  7143.       output_line_directive (ip, op, 1, same_file);
  7144.     }
  7145.   }
  7146.   return 0;
  7147. }
  7148.  
  7149. /*
  7150.  * evaluate a #if expression in BUF, of length LENGTH,
  7151.  * then parse the result as a C expression and return the value as an int.
  7152.  */
  7153. static HOST_WIDE_INT
  7154. eval_if_expression (buf, length)
  7155.      U_CHAR *buf;
  7156.      int length;
  7157. {
  7158.   FILE_BUF temp_obuf;
  7159.   HASHNODE *save_defined;
  7160.   HOST_WIDE_INT value;
  7161.  
  7162.   save_defined = install ((U_CHAR *) "defined", -1, T_SPEC_DEFINED,
  7163.               NULL_PTR, -1);
  7164.   pcp_inside_if = 1;
  7165.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
  7166.   pcp_inside_if = 0;
  7167.   delete_macro (save_defined);    /* clean up special symbol */
  7168.  
  7169.   value = parse_c_expression ((char *) temp_obuf.buf);
  7170.  
  7171.   free (temp_obuf.buf);
  7172.  
  7173.   return value;
  7174. }
  7175.  
  7176. /*
  7177.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  7178.  * then do or don't skip to the #endif/#else/#elif depending
  7179.  * on what directive is actually being processed.
  7180.  */
  7181.  
  7182. static int
  7183. do_xifdef (buf, limit, op, keyword)
  7184.      U_CHAR *buf, *limit;
  7185.      FILE_BUF *op;
  7186.      struct directive *keyword;
  7187. {
  7188.   int skip;
  7189.   FILE_BUF *ip = &instack[indepth];
  7190.   U_CHAR *end; 
  7191.   int start_of_file = 0;
  7192.   U_CHAR *control_macro = 0;
  7193.  
  7194.   /* Detect a #ifndef at start of file (not counting comments).  */
  7195.   if (ip->fname != 0 && keyword->type == T_IFNDEF) {
  7196.     U_CHAR *p = ip->buf;
  7197.     while (p != directive_start) {
  7198.       U_CHAR c = *p++;
  7199.       if (is_space[c])
  7200.     ;
  7201.       /* Make no special provision for backslash-newline here; this is
  7202.      slower if backslash-newlines are present, but it's correct,
  7203.      and it's not worth it to tune for the rare backslash-newline.  */
  7204.       else if (c == '/'
  7205.            && (*p == '*' || (cplusplus_comments && *p == '/'))) {
  7206.     /* Skip this comment.  */
  7207.     int junk = 0;
  7208.     U_CHAR *save_bufp = ip->bufp;
  7209.     ip->bufp = p + 1;
  7210.     p = skip_to_end_of_comment (ip, &junk, 1);
  7211.     ip->bufp = save_bufp;
  7212.       } else {
  7213.     goto fail;
  7214.       }
  7215.     }
  7216.     /* If we get here, this conditional is the beginning of the file.  */
  7217.     start_of_file = 1;
  7218.   fail: ;
  7219.   }
  7220.  
  7221.   /* Discard leading and trailing whitespace.  */
  7222.   SKIP_WHITE_SPACE (buf);
  7223.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  7224.  
  7225.   /* Find the end of the identifier at the beginning.  */
  7226.   for (end = buf; is_idchar[*end]; end++);
  7227.  
  7228.   if (end == buf) {
  7229.     skip = (keyword->type == T_IFDEF);
  7230.     if (! traditional)
  7231.       pedwarn (end == limit ? "`#%s' with no argument"
  7232.            : "`#%s' argument starts with punctuation",
  7233.            keyword->name);
  7234.   } else {
  7235.     HASHNODE *hp;
  7236.  
  7237.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  7238.       pedwarn ("`#%s' argument starts with a digit", keyword->name);
  7239.     else if (end != limit && !traditional)
  7240.       pedwarn ("garbage at end of `#%s' argument", keyword->name);
  7241.  
  7242.     hp = lookup (buf, end-buf, -1);
  7243.  
  7244.     if (pcp_outfile) {
  7245.       /* Output a precondition for this macro.  */
  7246.       if (hp &&
  7247.       (hp->type == T_CONST
  7248.        || (hp->type == T_MACRO && hp->value.defn->predefined)))
  7249.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  7250.       else {
  7251.     U_CHAR *cp = buf;
  7252.     fprintf (pcp_outfile, "#undef ");
  7253.     while (is_idchar[*cp]) /* Ick! */
  7254.       fputc (*cp++, pcp_outfile);
  7255.     putc ('\n', pcp_outfile);
  7256.       }
  7257.     }
  7258.  
  7259.     skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
  7260.     if (start_of_file && !skip) {
  7261.       control_macro = (U_CHAR *) xmalloc (end - buf + 1);
  7262.       bcopy ((char *) buf, (char *) control_macro, end - buf);
  7263.       control_macro[end - buf] = 0;
  7264.     }
  7265.   }
  7266.   
  7267.   conditional_skip (ip, skip, T_IF, control_macro, op);
  7268.   return 0;
  7269. }
  7270.  
  7271. /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  7272.    If this is a #ifndef starting at the beginning of a file,
  7273.    CONTROL_MACRO is the macro name tested by the #ifndef.
  7274.    Otherwise, CONTROL_MACRO is 0.  */
  7275.  
  7276. static void
  7277. conditional_skip (ip, skip, type, control_macro, op)
  7278.      FILE_BUF *ip;
  7279.      int skip;
  7280.      enum node_type type;
  7281.      U_CHAR *control_macro;
  7282.      FILE_BUF *op;
  7283. {
  7284.   IF_STACK_FRAME *temp;
  7285.  
  7286.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  7287.   temp->fname = ip->nominal_fname;
  7288.   temp->lineno = ip->lineno;
  7289.   temp->next = if_stack;
  7290.   temp->control_macro = control_macro;
  7291.   if_stack = temp;
  7292.  
  7293.   if_stack->type = type;
  7294.  
  7295.   if (skip != 0) {
  7296.     skip_if_group (ip, 0, op);
  7297.     return;
  7298.   } else {
  7299.     ++if_stack->if_succeeded;
  7300.     output_line_directive (ip, &outbuf, 1, same_file);
  7301.   }
  7302. }
  7303.  
  7304. /*
  7305.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  7306.  * leaves input ptr at the sharp sign found.
  7307.  * If ANY is nonzero, return at next directive of any sort.
  7308.  */
  7309. static void
  7310. skip_if_group (ip, any, op)
  7311.      FILE_BUF *ip;
  7312.      int any;
  7313.      FILE_BUF *op;
  7314. {
  7315.   register U_CHAR *bp = ip->bufp, *cp;
  7316.   register U_CHAR *endb = ip->buf + ip->length;
  7317.   struct directive *kt;
  7318.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  7319.   U_CHAR *beg_of_line = bp;
  7320.   register int ident_length;
  7321.   U_CHAR *ident, *after_ident;
  7322.   /* Save info about where the group starts.  */
  7323.   U_CHAR *beg_of_group = bp;
  7324.   int beg_lineno = ip->lineno;
  7325.  
  7326.   if (output_conditionals && op != 0) {
  7327.     char *ptr = "#failed\n";
  7328.     int len = strlen (ptr);
  7329.  
  7330.     if (op->bufp > op->buf && op->bufp[-1] != '\n')
  7331.       {
  7332.     *op->bufp++ = '\n';
  7333.     op->lineno++;
  7334.       }
  7335.     check_expand (op, len);
  7336.     bcopy (ptr, (char *) op->bufp, len);
  7337.     op->bufp += len;
  7338.     op->lineno++;
  7339.     output_line_directive (ip, op, 1, 0);
  7340.   }
  7341.  
  7342.   while (bp < endb) {
  7343.     switch (*bp++) {
  7344.     case '/':            /* possible comment */
  7345.       if (*bp == '\\' && bp[1] == '\n')
  7346.     newline_fix (bp);
  7347.       if (*bp == '*'
  7348.       || (cplusplus_comments && *bp == '/')) {
  7349.     ip->bufp = ++bp;
  7350.     bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
  7351.       }
  7352.       break;
  7353.     case '\"':
  7354.     case '\'':
  7355.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
  7356.                    NULL_PTR, NULL_PTR);
  7357.       break;
  7358.     case '\\':
  7359.       /* Char after backslash loses its special meaning.  */
  7360.       if (bp < endb) {
  7361.     if (*bp == '\n')
  7362.       ++ip->lineno;        /* But do update the line-count.  */
  7363.     bp++;
  7364.       }
  7365.       break;
  7366.     case '\n':
  7367.       ++ip->lineno;
  7368.       beg_of_line = bp;
  7369.       break;
  7370.     case '%':
  7371.       if (beg_of_line == 0 || traditional)
  7372.     break;
  7373.       ip->bufp = bp - 1;
  7374.       while (bp[0] == '\\' && bp[1] == '\n')
  7375.     bp += 2;
  7376.       if (*bp == ':')
  7377.     goto sharp_token;
  7378.       break;
  7379.     case '#':
  7380.       /* # keyword: a # must be first nonblank char on the line */
  7381.       if (beg_of_line == 0)
  7382.     break;
  7383.       ip->bufp = bp - 1;
  7384.     sharp_token:
  7385.       /* Scan from start of line, skipping whitespace, comments
  7386.      and backslash-newlines, and see if we reach this #.
  7387.      If not, this # is not special.  */
  7388.       bp = beg_of_line;
  7389.       /* If -traditional, require # to be at beginning of line.  */
  7390.       if (!traditional) {
  7391.     while (1) {
  7392.       if (is_hor_space[*bp])
  7393.         bp++;
  7394.       else if (*bp == '\\' && bp[1] == '\n')
  7395.         bp += 2;
  7396.       else if (*bp == '/' && bp[1] == '*') {
  7397.         bp += 2;
  7398.         while (!(*bp == '*' && bp[1] == '/'))
  7399.           bp++;
  7400.         bp += 2;
  7401.       }
  7402.       /* There is no point in trying to deal with C++ // comments here,
  7403.          because if there is one, then this # must be part of the
  7404.          comment and we would never reach here.  */
  7405.       else break;
  7406.     }
  7407.       }
  7408.       if (bp != ip->bufp) {
  7409.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  7410.     break;
  7411.       }
  7412.  
  7413.       bp = ip->bufp + 1;    /* Point after the '#' */
  7414.       if (ip->bufp[0] == '%') {
  7415.     /* Skip past the ':' again.  */
  7416.     while (*bp == '\\') {
  7417.       ip->lineno++;
  7418.       bp += 2;
  7419.     }
  7420.     bp++;
  7421.       }
  7422.  
  7423.       /* Skip whitespace and \-newline.  */
  7424.       while (1) {
  7425.     if (is_hor_space[*bp])
  7426.       bp++;
  7427.     else if (*bp == '\\' && bp[1] == '\n')
  7428.       bp += 2;
  7429.     else if (*bp == '/' && bp[1] == '*') {
  7430.       bp += 2;
  7431.       while (!(*bp == '*' && bp[1] == '/')) {
  7432.         if (*bp == '\n')
  7433.           ip->lineno++;
  7434.         bp++;
  7435.       }
  7436.       bp += 2;
  7437.     } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
  7438.       bp += 2;
  7439.       while (bp[-1] == '\\' || *bp != '\n') {
  7440.         if (*bp == '\n')
  7441.           ip->lineno++;
  7442.         bp++;
  7443.       }
  7444.         }
  7445.     else break;
  7446.       }
  7447.  
  7448.       cp = bp;
  7449.  
  7450.       /* Now find end of directive name.
  7451.      If we encounter a backslash-newline, exchange it with any following
  7452.      symbol-constituents so that we end up with a contiguous name.  */
  7453.  
  7454.       while (1) {
  7455.     if (is_idchar[*bp])
  7456.       bp++;
  7457.     else {
  7458.       if (*bp == '\\' && bp[1] == '\n')
  7459.         name_newline_fix (bp);
  7460.       if (is_idchar[*bp])
  7461.         bp++;
  7462.       else break;
  7463.     }
  7464.       }
  7465.       ident_length = bp - cp;
  7466.       ident = cp;
  7467.       after_ident = bp;
  7468.  
  7469.       /* A line of just `#' becomes blank.  */
  7470.  
  7471.       if (ident_length == 0 && *after_ident == '\n') {
  7472.     continue;
  7473.       }
  7474.  
  7475.       if (ident_length == 0 || !is_idstart[*ident]) {
  7476.     U_CHAR *p = ident;
  7477.     while (is_idchar[*p]) {
  7478.       if (*p < '0' || *p > '9')
  7479.         break;
  7480.       p++;
  7481.     }
  7482.     /* Handle # followed by a line number.  */
  7483.     if (p != ident && !is_idchar[*p]) {
  7484.       if (pedantic)
  7485.         pedwarn ("`#' followed by integer");
  7486.       continue;
  7487.     }
  7488.  
  7489.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  7490.     if (p == ident) {
  7491.       while (*p == '#' || is_hor_space[*p]) p++;
  7492.       if (*p == '\n') {
  7493.         if (pedantic && !lang_asm)
  7494.           pedwarn ("invalid preprocessing directive");
  7495.         continue;
  7496.       }
  7497.     }
  7498.  
  7499.     if (!lang_asm && pedantic)
  7500.       pedwarn ("invalid preprocessing directive name");
  7501.     continue;
  7502.       }
  7503.  
  7504.       for (kt = directive_table; kt->length >= 0; kt++) {
  7505.     IF_STACK_FRAME *temp;
  7506.     if (ident_length == kt->length
  7507.         && bcmp (cp, kt->name, kt->length) == 0) {
  7508.       /* If we are asked to return on next directive, do so now.  */
  7509.       if (any)
  7510.         goto done;
  7511.  
  7512.       switch (kt->type) {
  7513.       case T_IF:
  7514.       case T_IFDEF:
  7515.       case T_IFNDEF:
  7516.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  7517.         temp->next = if_stack;
  7518.         if_stack = temp;
  7519.         temp->lineno = ip->lineno;
  7520.         temp->fname = ip->nominal_fname;
  7521.         temp->type = kt->type;
  7522.         break;
  7523.       case T_ELSE:
  7524.       case T_ENDIF:
  7525.         if (pedantic && if_stack != save_if_stack)
  7526.           validate_else (bp);
  7527.       case T_ELIF:
  7528.         if (if_stack == instack[indepth].if_stack) {
  7529.           error ("`#%s' not within a conditional", kt->name);
  7530.           break;
  7531.         }
  7532.         else if (if_stack == save_if_stack)
  7533.           goto done;        /* found what we came for */
  7534.  
  7535.         if (kt->type != T_ENDIF) {
  7536.           if (if_stack->type == T_ELSE)
  7537.         error ("`#else' or `#elif' after `#else'");
  7538.           if_stack->type = kt->type;
  7539.           break;
  7540.         }
  7541.  
  7542.         temp = if_stack;
  7543.         if_stack = if_stack->next;
  7544.         free (temp);
  7545.         break;
  7546.  
  7547.        default:
  7548.         break;
  7549.       }
  7550.       break;
  7551.     }
  7552.       }
  7553.       /* Don't let erroneous code go by.  */
  7554.       if (kt->length < 0 && !lang_asm && pedantic)
  7555.     pedwarn ("invalid preprocessing directive name");
  7556.     }
  7557.   }
  7558.  
  7559.   ip->bufp = bp;
  7560.   /* after this returns, rescan will exit because ip->bufp
  7561.      now points to the end of the buffer.
  7562.      rescan is responsible for the error message also.  */
  7563.  
  7564.  done:
  7565.   if (output_conditionals && op != 0) {
  7566.     char *ptr = "#endfailed\n";
  7567.     int len = strlen (ptr);
  7568.  
  7569.     if (op->bufp > op->buf && op->bufp[-1] != '\n')
  7570.       {
  7571.     *op->bufp++ = '\n';
  7572.     op->lineno++;
  7573.       }
  7574.     check_expand (op, beg_of_line - beg_of_group);
  7575.     bcopy ((char *) beg_of_group, (char *) op->bufp,
  7576.        beg_of_line - beg_of_group);
  7577.     op->bufp += beg_of_line - beg_of_group;
  7578.     op->lineno += ip->lineno - beg_lineno;
  7579.     check_expand (op, len);
  7580.     bcopy (ptr, (char *) op->bufp, len);
  7581.     op->bufp += len;
  7582.     op->lineno++;
  7583.   }
  7584. }
  7585.  
  7586. /*
  7587.  * handle a #else directive.  Do this by just continuing processing
  7588.  * without changing  if_stack ;  this is so that the error message
  7589.  * for missing #endif's etc. will point to the original #if.  It
  7590.  * is possible that something different would be better.
  7591.  */
  7592.  
  7593. static int
  7594. do_else (buf, limit, op, keyword)
  7595.      U_CHAR *buf, *limit;
  7596.      FILE_BUF *op;
  7597.      struct directive *keyword;
  7598. {
  7599.   FILE_BUF *ip = &instack[indepth];
  7600.  
  7601.   if (pedantic) {
  7602.     SKIP_WHITE_SPACE (buf);
  7603.     if (buf != limit)
  7604.       pedwarn ("text following `#else' violates ANSI standard");
  7605.   }
  7606.  
  7607.   if (if_stack == instack[indepth].if_stack) {
  7608.     error ("`#else' not within a conditional");
  7609.     return 0;
  7610.   } else {
  7611.     /* #ifndef can't have its special treatment for containing the whole file
  7612.        if it has a #else clause.  */
  7613.     if_stack->control_macro = 0;
  7614.  
  7615.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  7616.       error ("`#else' after `#else'");
  7617.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  7618.       if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
  7619.     fprintf (stderr, ", file %s", if_stack->fname);
  7620.       fprintf (stderr, ")\n");
  7621.     }
  7622.     if_stack->type = T_ELSE;
  7623.   }
  7624.  
  7625.   if (if_stack->if_succeeded)
  7626.     skip_if_group (ip, 0, op);
  7627.   else {
  7628.     ++if_stack->if_succeeded;    /* continue processing input */
  7629.     output_line_directive (ip, op, 1, same_file);
  7630.   }
  7631.   return 0;
  7632. }
  7633.  
  7634. /*
  7635.  * unstack after #endif directive
  7636.  */
  7637.  
  7638. static int
  7639. do_endif (buf, limit, op, keyword)
  7640.      U_CHAR *buf, *limit;
  7641.      FILE_BUF *op;
  7642.      struct directive *keyword;
  7643. {
  7644.   if (pedantic) {
  7645.     SKIP_WHITE_SPACE (buf);
  7646.     if (buf != limit)
  7647.       pedwarn ("text following `#endif' violates ANSI standard");
  7648.   }
  7649.  
  7650.   if (if_stack == instack[indepth].if_stack)
  7651.     error ("unbalanced `#endif'");
  7652.   else {
  7653.     IF_STACK_FRAME *temp = if_stack;
  7654.     if_stack = if_stack->next;
  7655.     if (temp->control_macro != 0) {
  7656.       /* This #endif matched a #ifndef at the start of the file.
  7657.      See if it is at the end of the file.  */
  7658.       FILE_BUF *ip = &instack[indepth];
  7659.       U_CHAR *p = ip->bufp;
  7660.       U_CHAR *ep = ip->buf + ip->length;
  7661.  
  7662.       while (p != ep) {
  7663.     U_CHAR c = *p++;
  7664.     if (!is_space[c]) {
  7665.       if (c == '/'
  7666.           && (*p == '*' || (cplusplus_comments && *p == '/'))) {
  7667.         /* Skip this comment.  */
  7668.         int junk = 0;
  7669.         U_CHAR *save_bufp = ip->bufp;
  7670.         ip->bufp = p + 1;
  7671.         p = skip_to_end_of_comment (ip, &junk, 1);
  7672.         ip->bufp = save_bufp;
  7673.       } else
  7674.         goto fail;
  7675.     }
  7676.       }
  7677.       /* If we get here, this #endif ends a #ifndef
  7678.      that contains all of the file (aside from whitespace).
  7679.      Arrange not to include the file again
  7680.      if the macro that was tested is defined.
  7681.  
  7682.      Do not do this for the top-level file in a -include or any
  7683.      file in a -imacros.  */
  7684.       if (indepth != 0
  7685.       && ! (indepth == 1 && no_record_file)
  7686.       && ! (no_record_file && no_output))
  7687.     record_control_macro (ip->fname, temp->control_macro);
  7688.     fail: ;
  7689.     }
  7690.     free (temp);
  7691.     output_line_directive (&instack[indepth], op, 1, same_file);
  7692.   }
  7693.   return 0;
  7694. }
  7695.  
  7696. /* When an #else or #endif is found while skipping failed conditional,
  7697.    if -pedantic was specified, this is called to warn about text after
  7698.    the directive name.  P points to the first char after the directive name.  */
  7699.  
  7700. static void
  7701. validate_else (p)
  7702.      register U_CHAR *p;
  7703. {
  7704.   /* Advance P over whitespace and comments.  */
  7705.   while (1) {
  7706.     if (*p == '\\' && p[1] == '\n')
  7707.       p += 2;
  7708.     if (is_hor_space[*p])
  7709.       p++;
  7710.     else if (*p == '/') {
  7711.       if (p[1] == '\\' && p[2] == '\n')
  7712.     newline_fix (p + 1);
  7713.       if (p[1] == '*') {
  7714.     p += 2;
  7715.     /* Don't bother warning about unterminated comments
  7716.        since that will happen later.  Just be sure to exit.  */
  7717.     while (*p) {
  7718.       if (p[1] == '\\' && p[2] == '\n')
  7719.         newline_fix (p + 1);
  7720.       if (*p == '*' && p[1] == '/') {
  7721.         p += 2;
  7722.         break;
  7723.       }
  7724.       p++;
  7725.     }
  7726.       }
  7727.       else if (cplusplus_comments && p[1] == '/') {
  7728.     p += 2;
  7729.     while (*p && (*p != '\n' || p[-1] == '\\'))
  7730.       p++;
  7731.       }
  7732.     } else break;
  7733.   }
  7734.   if (*p && *p != '\n')
  7735.     pedwarn ("text following `#else' or `#endif' violates ANSI standard");
  7736. }
  7737.  
  7738. /* Skip a comment, assuming the input ptr immediately follows the
  7739.    initial slash-star.  Bump *LINE_COUNTER for each newline.
  7740.    (The canonical line counter is &ip->lineno.)
  7741.    Don't use this routine (or the next one) if bumping the line
  7742.    counter is not sufficient to deal with newlines in the string.
  7743.  
  7744.    If NOWARN is nonzero, don't warn about slash-star inside a comment.
  7745.    This feature is useful when processing a comment that is going to be
  7746.    processed or was processed at another point in the preprocessor,
  7747.    to avoid a duplicate warning.  Likewise for unterminated comment errors.  */
  7748.  
  7749. static U_CHAR *
  7750. skip_to_end_of_comment (ip, line_counter, nowarn)
  7751.      register FILE_BUF *ip;
  7752.      int *line_counter;        /* place to remember newlines, or NULL */
  7753.      int nowarn;
  7754. {
  7755.   register U_CHAR *limit = ip->buf + ip->length;
  7756.   register U_CHAR *bp = ip->bufp;
  7757.   FILE_BUF *op = &outbuf;    /* JF */
  7758.   int output = put_out_comments && !line_counter;
  7759.   int start_line = line_counter ? *line_counter : 0;
  7760.  
  7761.     /* JF this line_counter stuff is a crock to make sure the
  7762.        comment is only put out once, no matter how many times
  7763.        the comment is skipped.  It almost works */
  7764.   if (output) {
  7765.     *op->bufp++ = '/';
  7766.     *op->bufp++ = '*';
  7767.   }
  7768.   if (cplusplus_comments && bp[-1] == '/') {
  7769.     if (output) {
  7770.       while (bp < limit) {
  7771.     *op->bufp++ = *bp;
  7772.     if (*bp == '\n' && bp[-1] != '\\')
  7773.       break;
  7774.     if (*bp == '\n') {
  7775.       ++*line_counter;
  7776.       ++op->lineno;
  7777.     }
  7778.     bp++;
  7779.       }
  7780.       op->bufp[-1] = '*';
  7781.       *op->bufp++ = '/';
  7782.       *op->bufp++ = '\n';
  7783.     } else {
  7784.       while (bp < limit) {
  7785.     if (bp[-1] != '\\' && *bp == '\n') {
  7786.       break;
  7787.     } else {
  7788.       if (*bp == '\n' && line_counter)
  7789.         ++*line_counter;
  7790.       bp++;
  7791.     }
  7792.       }
  7793.     }
  7794.     ip->bufp = bp;
  7795.     return bp;
  7796.   }
  7797.   while (bp < limit) {
  7798.     if (output)
  7799.       *op->bufp++ = *bp;
  7800.     switch (*bp++) {
  7801.     case '/':
  7802.       if (warn_comments && !nowarn && bp < limit && *bp == '*')
  7803.     warning ("`/*' within comment");
  7804.       break;
  7805.     case '\n':
  7806.       /* If this is the end of the file, we have an unterminated comment.
  7807.      Don't swallow the newline.  We are guaranteed that there will be a
  7808.      trailing newline and various pieces assume it's there.  */
  7809.       if (bp == limit)
  7810.     {
  7811.       --bp;
  7812.       --limit;
  7813.       break;
  7814.     }
  7815.       if (line_counter != NULL)
  7816.     ++*line_counter;
  7817.       if (output)
  7818.     ++op->lineno;
  7819.       break;
  7820.     case '*':
  7821.       if (*bp == '\\' && bp[1] == '\n')
  7822.     newline_fix (bp);
  7823.       if (*bp == '/') {
  7824.         if (output)
  7825.       *op->bufp++ = '/';
  7826.     ip->bufp = ++bp;
  7827.     return bp;
  7828.       }
  7829.       break;
  7830.     }
  7831.   }
  7832.  
  7833.   if (!nowarn)
  7834.     error_with_line (line_for_error (start_line), "unterminated comment");
  7835.   ip->bufp = bp;
  7836.   return bp;
  7837. }
  7838.  
  7839. /*
  7840.  * Skip over a quoted string.  BP points to the opening quote.
  7841.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  7842.  * START_LINE is the line number of the starting point (but it need
  7843.  * not be valid if the starting point is inside a macro expansion).
  7844.  *
  7845.  * The input stack state is not changed.
  7846.  *
  7847.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  7848.  * for each newline passed.
  7849.  *
  7850.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  7851.  * if we pass a backslash-newline.
  7852.  *
  7853.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  7854.  */
  7855. static U_CHAR *
  7856. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  7857.      register U_CHAR *bp;
  7858.      register U_CHAR *limit;
  7859.      int start_line;
  7860.      int *count_newlines;
  7861.      int *backslash_newlines_p;
  7862.      int *eofp;
  7863. {
  7864.   register U_CHAR c, match;
  7865.  
  7866.   match = *bp++;
  7867.   while (1) {
  7868.     if (bp >= limit) {
  7869.       error_with_line (line_for_error (start_line),
  7870.                "unterminated string or character constant");
  7871.       error_with_line (multiline_string_line,
  7872.                "possible real start of unterminated constant");
  7873.       multiline_string_line = 0;
  7874.       if (eofp)
  7875.     *eofp = 1;
  7876.       break;
  7877.     }
  7878.     c = *bp++;
  7879.     if (c == '\\') {
  7880.       while (*bp == '\\' && bp[1] == '\n') {
  7881.     if (backslash_newlines_p)
  7882.       *backslash_newlines_p = 1;
  7883.     if (count_newlines)
  7884.       ++*count_newlines;
  7885.     bp += 2;
  7886.       }
  7887.       if (*bp == '\n' && count_newlines) {
  7888.     if (backslash_newlines_p)
  7889.       *backslash_newlines_p = 1;
  7890.     ++*count_newlines;
  7891.       }
  7892.       bp++;
  7893.     } else if (c == '\n') {
  7894.       if (traditional) {
  7895.      /* Unterminated strings and character constants are 'valid'.  */
  7896.      bp--;    /* Don't consume the newline. */
  7897.      if (eofp)
  7898.        *eofp = 1;
  7899.      break;
  7900.       }
  7901.       if (pedantic || match == '\'') {
  7902.     error_with_line (line_for_error (start_line),
  7903.              "unterminated string or character constant");
  7904.     bp--;
  7905.     if (eofp)
  7906.       *eofp = 1;
  7907.     break;
  7908.       }
  7909.       /* If not traditional, then allow newlines inside strings.  */
  7910.       if (count_newlines)
  7911.     ++*count_newlines;
  7912.       if (multiline_string_line == 0)
  7913.     multiline_string_line = start_line;
  7914.     } else if (c == match)
  7915.       break;
  7916.   }
  7917.   return bp;
  7918. }
  7919.  
  7920. /* Place into DST a quoted string representing the string SRC.
  7921.    Return the address of DST's terminating null.  */
  7922. static char *
  7923. quote_string (dst, src)
  7924.      char *dst, *src;
  7925. {
  7926.   U_CHAR c;
  7927.  
  7928.   *dst++ = '\"';
  7929.   for (;;)
  7930.     switch ((c = *src++))
  7931.       {
  7932.       default:
  7933.         if (isprint (c))
  7934.       *dst++ = c;
  7935.     else
  7936.       {
  7937.         sprintf (dst, "\\%03o", c);
  7938.         dst += 4;
  7939.       }
  7940.     break;
  7941.  
  7942.       case '\"':
  7943.       case '\\':
  7944.     *dst++ = '\\';
  7945.     *dst++ = c;
  7946.     break;
  7947.       
  7948.       case '\0':
  7949.     *dst++ = '\"';
  7950.     *dst = '\0';
  7951.     return dst;
  7952.       }
  7953. }
  7954.  
  7955. /* Skip across a group of balanced parens, starting from IP->bufp.
  7956.    IP->bufp is updated.  Use this with IP->bufp pointing at an open-paren.
  7957.  
  7958.    This does not handle newlines, because it's used for the arg of #if,
  7959.    where there aren't any newlines.  Also, backslash-newline can't appear.  */
  7960.  
  7961. static U_CHAR *
  7962. skip_paren_group (ip)
  7963.      register FILE_BUF *ip;
  7964. {
  7965.   U_CHAR *limit = ip->buf + ip->length;
  7966.   U_CHAR *p = ip->bufp;
  7967.   int depth = 0;
  7968.   int lines_dummy = 0;
  7969.  
  7970.   while (p != limit) {
  7971.     int c = *p++;
  7972.     switch (c) {
  7973.     case '(':
  7974.       depth++;
  7975.       break;
  7976.  
  7977.     case ')':
  7978.       depth--;
  7979.       if (depth == 0)
  7980.     return ip->bufp = p;
  7981.       break;
  7982.  
  7983.     case '/':
  7984.       if (*p == '*') {
  7985.     ip->bufp = p;
  7986.     p = skip_to_end_of_comment (ip, &lines_dummy, 0);
  7987.     p = ip->bufp;
  7988.       }
  7989.  
  7990.     case '"':
  7991.     case '\'':
  7992.       {
  7993.     int eofp = 0;
  7994.     p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  7995.     if (eofp)
  7996.       return ip->bufp = p;
  7997.       }
  7998.       break;
  7999.     }
  8000.   }
  8001.  
  8002.   ip->bufp = p;
  8003.   return p;
  8004. }
  8005.  
  8006. /*
  8007.  * write out a #line directive, for instance, after an #include file.
  8008.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  8009.  * appear to be a no-op, and we can output a few newlines instead
  8010.  * if we want to increase the line number by a small amount.
  8011.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  8012.  */
  8013.  
  8014. static void
  8015. output_line_directive (ip, op, conditional, file_change)
  8016.      FILE_BUF *ip, *op;
  8017.      int conditional;
  8018.      enum file_change_code file_change;
  8019. {
  8020.   int len;
  8021.   char *line_directive_buf, *line_end;
  8022.  
  8023.   if (no_line_directives
  8024.       || ip->fname == NULL
  8025.       || no_output) {
  8026.     op->lineno = ip->lineno;
  8027.     return;
  8028.   }
  8029.  
  8030.   if (conditional) {
  8031.     if (ip->lineno == op->lineno)
  8032.       return;
  8033.  
  8034.     /* If the inherited line number is a little too small,
  8035.        output some newlines instead of a #line directive.  */
  8036.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  8037.       check_expand (op, 10);
  8038.       while (ip->lineno > op->lineno) {
  8039.     *op->bufp++ = '\n';
  8040.     op->lineno++;
  8041.       }
  8042.       return;
  8043.     }
  8044.   }
  8045.  
  8046.   /* Don't output a line number of 0 if we can help it.  */
  8047.   if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
  8048.       && *ip->bufp == '\n') {
  8049.     ip->lineno++;
  8050.     ip->bufp++;
  8051.   }
  8052.  
  8053.   line_directive_buf = (char *) alloca (4 * strlen (ip->nominal_fname) + 100);
  8054.   sprintf (line_directive_buf, "# %d ", ip->lineno);
  8055.   line_end = quote_string (line_directive_buf + strlen (line_directive_buf),
  8056.                ip->nominal_fname);
  8057.   if (file_change != same_file) {
  8058.     *line_end++ = ' ';
  8059.     *line_end++ = file_change == enter_file ? '1' : '2';
  8060.   }
  8061.   /* Tell cc1 if following text comes from a system header file.  */
  8062.   if (ip->system_header_p) {
  8063.     *line_end++ = ' ';
  8064.     *line_end++ = '3';
  8065.   }
  8066. #ifndef NO_IMPLICIT_EXTERN_C
  8067.   /* Tell cc1plus if following text should be treated as C.  */
  8068.   if (ip->system_header_p == 2 && cplusplus) {
  8069.     *line_end++ = ' ';
  8070.     *line_end++ = '4';
  8071.   }
  8072. #endif
  8073.   *line_end++ = '\n';
  8074.   len = line_end - line_directive_buf;
  8075.   check_expand (op, len + 1);
  8076.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  8077.     *op->bufp++ = '\n';
  8078.   bcopy ((char *) line_directive_buf, (char *) op->bufp, len);
  8079.   op->bufp += len;
  8080.   op->lineno = ip->lineno;
  8081. }
  8082.  
  8083. /* This structure represents one parsed argument in a macro call.
  8084.    `raw' points to the argument text as written (`raw_length' is its length).
  8085.    `expanded' points to the argument's macro-expansion
  8086.    (its length is `expand_length').
  8087.    `stringified_length' is the length the argument would have
  8088.    if stringified.
  8089.    `use_count' is the number of times this macro arg is substituted
  8090.    into the macro.  If the actual use count exceeds 10, 
  8091.    the value stored is 10.
  8092.    `free1' and `free2', if nonzero, point to blocks to be freed
  8093.    when the macro argument data is no longer needed.  */
  8094.  
  8095. struct argdata {
  8096.   U_CHAR *raw, *expanded;
  8097.   int raw_length, expand_length;
  8098.   int stringified_length;
  8099.   U_CHAR *free1, *free2;
  8100.   char newlines;
  8101.   char use_count;
  8102. };
  8103.  
  8104. /* Expand a macro call.
  8105.    HP points to the symbol that is the macro being called.
  8106.    Put the result of expansion onto the input stack
  8107.    so that subsequent input by our caller will use it.
  8108.  
  8109.    If macro wants arguments, caller has already verified that
  8110.    an argument list follows; arguments come from the input stack.  */
  8111.  
  8112. static void
  8113. macroexpand (hp, op)
  8114.      HASHNODE *hp;
  8115.      FILE_BUF *op;
  8116. {
  8117.   int nargs;
  8118.   DEFINITION *defn = hp->value.defn;
  8119.   register U_CHAR *xbuf;
  8120.   int xbuf_len;
  8121.   int start_line = instack[indepth].lineno;
  8122.   int rest_args, rest_zero;
  8123.  
  8124.   CHECK_DEPTH (return;);
  8125.  
  8126.   /* it might not actually be a macro.  */
  8127.   if (hp->type != T_MACRO) {
  8128.     special_symbol (hp, op);
  8129.     return;
  8130.   }
  8131.  
  8132.   /* This macro is being used inside a #if, which means it must be */
  8133.   /* recorded as a precondition.  */
  8134.   if (pcp_inside_if && pcp_outfile && defn->predefined)
  8135.     dump_single_macro (hp, pcp_outfile);
  8136.   
  8137.   nargs = defn->nargs;
  8138.  
  8139.   if (nargs >= 0) {
  8140.     register int i;
  8141.     struct argdata *args;
  8142.     char *parse_error = 0;
  8143.  
  8144.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  8145.  
  8146.     for (i = 0; i < nargs; i++) {
  8147.       args[i].raw = (U_CHAR *) "";
  8148.       args[i].expanded = 0;
  8149.       args[i].raw_length = args[i].expand_length
  8150.     = args[i].stringified_length = 0;
  8151.       args[i].free1 = args[i].free2 = 0;
  8152.       args[i].use_count = 0;
  8153.     }
  8154.  
  8155.     /* Parse all the macro args that are supplied.  I counts them.
  8156.        The first NARGS args are stored in ARGS.
  8157.        The rest are discarded.
  8158.        If rest_args is set then we assume macarg absorbed the rest of the args.
  8159.        */
  8160.     i = 0;
  8161.     rest_args = 0;
  8162.     do {
  8163.       /* Discard the open-parenthesis or comma before the next arg.  */
  8164.       ++instack[indepth].bufp;
  8165.       if (rest_args)
  8166.     continue;
  8167.       if (i < nargs || (nargs == 0 && i == 0)) {
  8168.     /* if we are working on last arg which absorbs rest of args... */
  8169.     if (i == nargs - 1 && defn->rest_args)
  8170.       rest_args = 1;
  8171.     parse_error = macarg (&args[i], rest_args);
  8172.       }
  8173.       else
  8174.     parse_error = macarg (NULL_PTR, 0);
  8175.       if (parse_error) {
  8176.     error_with_line (line_for_error (start_line), parse_error);
  8177.     break;
  8178.       }
  8179.       i++;
  8180.     } while (*instack[indepth].bufp != ')');
  8181.  
  8182.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  8183.     if (i == 1) {
  8184.       register U_CHAR *bp = args[0].raw;
  8185.       register U_CHAR *lim = bp + args[0].raw_length;
  8186.       /* cpp.texi says for foo ( ) we provide one argument.
  8187.      However, if foo wants just 0 arguments, treat this as 0.  */
  8188.       if (nargs == 0)
  8189.     while (bp != lim && is_space[*bp]) bp++;
  8190.       if (bp == lim)
  8191.     i = 0;
  8192.     }
  8193.  
  8194.     /* Don't output an error message if we have already output one for
  8195.        a parse error above.  */
  8196.     rest_zero = 0;
  8197.     if (nargs == 0 && i > 0) {
  8198.       if (! parse_error)
  8199.     error ("arguments given to macro `%s'", hp->name);
  8200.     } else if (i < nargs) {
  8201.       /* traditional C allows foo() if foo wants one argument.  */
  8202.       if (nargs == 1 && i == 0 && traditional)
  8203.     ;
  8204.       /* the rest args token is allowed to absorb 0 tokens */
  8205.       else if (i == nargs - 1 && defn->rest_args)
  8206.     rest_zero = 1;
  8207.       else if (parse_error)
  8208.     ;
  8209.       else if (i == 0)
  8210.     error ("macro `%s' used without args", hp->name);
  8211.       else if (i == 1)
  8212.     error ("macro `%s' used with just one arg", hp->name);
  8213.       else
  8214.     error ("macro `%s' used with only %d args", hp->name, i);
  8215.     } else if (i > nargs) {
  8216.       if (! parse_error)
  8217.     error ("macro `%s' used with too many (%d) args", hp->name, i);
  8218.     }
  8219.  
  8220.     /* Swallow the closeparen.  */
  8221.     ++instack[indepth].bufp;
  8222.  
  8223.     /* If macro wants zero args, we parsed the arglist for checking only.
  8224.        Read directly from the macro definition.  */
  8225.     if (nargs == 0) {
  8226.       xbuf = defn->expansion;
  8227.       xbuf_len = defn->length;
  8228.     } else {
  8229.       register U_CHAR *exp = defn->expansion;
  8230.       register int offset;    /* offset in expansion,
  8231.                    copied a piece at a time */
  8232.       register int totlen;    /* total amount of exp buffer filled so far */
  8233.  
  8234.       register struct reflist *ap, *last_ap;
  8235.  
  8236.       /* Macro really takes args.  Compute the expansion of this call.  */
  8237.  
  8238.       /* Compute length in characters of the macro's expansion.
  8239.      Also count number of times each arg is used.  */
  8240.       xbuf_len = defn->length;
  8241.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  8242.     if (ap->stringify)
  8243.       xbuf_len += args[ap->argno].stringified_length;
  8244.     else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional)
  8245.       /* Add 4 for two newline-space markers to prevent
  8246.          token concatenation.  */
  8247.       xbuf_len += args[ap->argno].raw_length + 4;
  8248.     else {
  8249.       /* We have an ordinary (expanded) occurrence of the arg.
  8250.          So compute its expansion, if we have not already.  */
  8251.       if (args[ap->argno].expanded == 0) {
  8252.         FILE_BUF obuf;
  8253.         obuf = expand_to_temp_buffer (args[ap->argno].raw,
  8254.                       args[ap->argno].raw + args[ap->argno].raw_length,
  8255.                       1, 0);
  8256.  
  8257.         args[ap->argno].expanded = obuf.buf;
  8258.         args[ap->argno].expand_length = obuf.length;
  8259.         args[ap->argno].free2 = obuf.buf;
  8260.       }
  8261.  
  8262.       /* Add 4 for two newline-space markers to prevent
  8263.          token concatenation.  */
  8264.       xbuf_len += args[ap->argno].expand_length + 4;
  8265.     }
  8266.     if (args[ap->argno].use_count < 10)
  8267.       args[ap->argno].use_count++;
  8268.       }
  8269.  
  8270.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  8271.  
  8272.       /* Generate in XBUF the complete expansion
  8273.      with arguments substituted in.
  8274.      TOTLEN is the total size generated so far.
  8275.      OFFSET is the index in the definition
  8276.      of where we are copying from.  */
  8277.       offset = totlen = 0;
  8278.       for (last_ap = NULL, ap = defn->pattern; ap != NULL;
  8279.        last_ap = ap, ap = ap->next) {
  8280.     register struct argdata *arg = &args[ap->argno];
  8281.     int count_before = totlen;
  8282.  
  8283.     /* Add chars to XBUF.  */
  8284.     for (i = 0; i < ap->nchars; i++, offset++)
  8285.       xbuf[totlen++] = exp[offset];
  8286.  
  8287.     /* If followed by an empty rest arg with concatenation,
  8288.        delete the last run of nonwhite chars.  */
  8289.     if (rest_zero && totlen > count_before
  8290.         && ((ap->rest_args && ap->raw_before != 0)
  8291.         || (last_ap != NULL && last_ap->rest_args
  8292.             && last_ap->raw_after != 0))) {
  8293.       /* Delete final whitespace.  */
  8294.       while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
  8295.         totlen--;
  8296.       }
  8297.  
  8298.       /* Delete the nonwhites before them.  */
  8299.       while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
  8300.         totlen--;
  8301.       }
  8302.     }
  8303.  
  8304.     if (ap->stringify != 0) {
  8305.       int arglen = arg->raw_length;
  8306.       int escaped = 0;
  8307.       int in_string = 0;
  8308.       int c;
  8309.       i = 0;
  8310.       while (i < arglen
  8311.          && (c = arg->raw[i], is_space[c]))
  8312.         i++;
  8313.       while (i < arglen
  8314.          && (c = arg->raw[arglen - 1], is_space[c]))
  8315.         arglen--;
  8316.       if (!traditional)
  8317.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  8318.       for (; i < arglen; i++) {
  8319.         c = arg->raw[i];
  8320.  
  8321.         /* Special markers Newline Space
  8322.            generate nothing for a stringified argument.  */
  8323.         if (c == '\n' && arg->raw[i+1] != '\n') {
  8324.           i++;
  8325.           continue;
  8326.         }
  8327.  
  8328.         /* Internal sequences of whitespace are replaced by one space
  8329.            except within an string or char token.  */
  8330.         if (! in_string
  8331.         && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
  8332.           while (1) {
  8333.         /* Note that Newline Space does occur within whitespace
  8334.            sequences; consider it part of the sequence.  */
  8335.         if (c == '\n' && is_space[arg->raw[i+1]])
  8336.           i += 2;
  8337.         else if (c != '\n' && is_space[c])
  8338.           i++;
  8339.         else break;
  8340.         c = arg->raw[i];
  8341.           }
  8342.           i--;
  8343.           c = ' ';
  8344.         }
  8345.  
  8346.         if (escaped)
  8347.           escaped = 0;
  8348.         else {
  8349.           if (c == '\\')
  8350.         escaped = 1;
  8351.           if (in_string) {
  8352.         if (c == in_string)
  8353.           in_string = 0;
  8354.           } else if (c == '\"' || c == '\'')
  8355.         in_string = c;
  8356.         }
  8357.  
  8358.         /* Escape these chars */
  8359.         if (c == '\"' || (in_string && c == '\\'))
  8360.           xbuf[totlen++] = '\\';
  8361.         if (isprint (c))
  8362.           xbuf[totlen++] = c;
  8363.         else {
  8364.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  8365.           totlen += 4;
  8366.         }
  8367.       }
  8368.       if (!traditional)
  8369.         xbuf[totlen++] = '\"'; /* insert ending quote */
  8370.     } else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional) {
  8371.       U_CHAR *p1 = arg->raw;
  8372.       U_CHAR *l1 = p1 + arg->raw_length;
  8373.       if (ap->raw_before != 0) {
  8374.         while (p1 != l1 && is_space[*p1]) p1++;
  8375.         while (p1 != l1 && is_idchar[*p1])
  8376.           xbuf[totlen++] = *p1++;
  8377.         /* Delete any no-reexpansion marker that follows
  8378.            an identifier at the beginning of the argument
  8379.            if the argument is concatenated with what precedes it.  */
  8380.         if (p1[0] == '\n' && p1[1] == '-')
  8381.           p1 += 2;
  8382.       } else if (!traditional) {
  8383.       /* Ordinary expanded use of the argument.
  8384.          Put in newline-space markers to prevent token pasting.  */
  8385.         xbuf[totlen++] = '\n';
  8386.         xbuf[totlen++] = ' ';
  8387.       }
  8388.       if (ap->raw_after != 0) {
  8389.         /* Arg is concatenated after: delete trailing whitespace,
  8390.            whitespace markers, and no-reexpansion markers.  */
  8391.         while (p1 != l1) {
  8392.           if (is_space[l1[-1]]) l1--;
  8393.           else if (l1[-1] == '-') {
  8394.         U_CHAR *p2 = l1 - 1;
  8395.         /* If a `-' is preceded by an odd number of newlines then it
  8396.            and the last newline are a no-reexpansion marker.  */
  8397.         while (p2 != p1 && p2[-1] == '\n') p2--;
  8398.         if ((l1 - 1 - p2) & 1) {
  8399.           l1 -= 2;
  8400.         }
  8401.         else break;
  8402.           }
  8403.           else break;
  8404.         }
  8405.       }
  8406.  
  8407.       bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
  8408.       totlen += l1 - p1;
  8409.       if (!traditional && ap->raw_after == 0) {
  8410.         /* Ordinary expanded use of the argument.
  8411.            Put in newline-space markers to prevent token pasting.  */
  8412.         xbuf[totlen++] = '\n';
  8413.         xbuf[totlen++] = ' ';
  8414.       }
  8415.     } else {
  8416.       /* Ordinary expanded use of the argument.
  8417.          Put in newline-space markers to prevent token pasting.  */
  8418.       if (!traditional) {
  8419.         xbuf[totlen++] = '\n';
  8420.         xbuf[totlen++] = ' ';
  8421.       }
  8422.       bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
  8423.          arg->expand_length);
  8424.       totlen += arg->expand_length;
  8425.       if (!traditional) {
  8426.         xbuf[totlen++] = '\n';
  8427.         xbuf[totlen++] = ' ';
  8428.       }
  8429.       /* If a macro argument with newlines is used multiple times,
  8430.          then only expand the newlines once.  This avoids creating output
  8431.          lines which don't correspond to any input line, which confuses
  8432.          gdb and gcov.  */
  8433.       if (arg->use_count > 1 && arg->newlines > 0) {
  8434.         /* Don't bother doing change_newlines for subsequent
  8435.            uses of arg.  */
  8436.         arg->use_count = 1;
  8437.         arg->expand_length
  8438.           = change_newlines (arg->expanded, arg->expand_length);
  8439.       }
  8440.     }
  8441.  
  8442.     if (totlen > xbuf_len)
  8443.       abort ();
  8444.       }
  8445.  
  8446.       /* if there is anything left of the definition
  8447.      after handling the arg list, copy that in too. */
  8448.  
  8449.       for (i = offset; i < defn->length; i++) {
  8450.     /* if we've reached the end of the macro */
  8451.     if (exp[i] == ')')
  8452.       rest_zero = 0;
  8453.     if (! (rest_zero && last_ap != NULL && last_ap->rest_args
  8454.            && last_ap->raw_after != 0))
  8455.       xbuf[totlen++] = exp[i];
  8456.       }
  8457.  
  8458.       xbuf[totlen] = 0;
  8459.       xbuf_len = totlen;
  8460.  
  8461.       for (i = 0; i < nargs; i++) {
  8462.     if (args[i].free1 != 0)
  8463.       free (args[i].free1);
  8464.     if (args[i].free2 != 0)
  8465.       free (args[i].free2);
  8466.       }
  8467.     }
  8468.   } else {
  8469.     xbuf = defn->expansion;
  8470.     xbuf_len = defn->length;
  8471.   }
  8472.  
  8473.   /* Now put the expansion on the input stack
  8474.      so our caller will commence reading from it.  */
  8475.   {
  8476.     register FILE_BUF *ip2;
  8477.  
  8478.     ip2 = &instack[++indepth];
  8479.  
  8480.     ip2->fname = 0;
  8481.     ip2->nominal_fname = 0;
  8482.     /* This may not be exactly correct, but will give much better error
  8483.        messages for nested macro calls than using a line number of zero.  */
  8484.     ip2->lineno = start_line;
  8485.     ip2->buf = xbuf;
  8486.     ip2->length = xbuf_len;
  8487.     ip2->bufp = xbuf;
  8488.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  8489.     ip2->macro = hp;
  8490.     ip2->if_stack = if_stack;
  8491.     ip2->system_header_p = 0;
  8492.  
  8493.     /* Recursive macro use sometimes works traditionally.
  8494.        #define foo(x,y) bar (x (y,0), y)
  8495.        foo (foo, baz)  */
  8496.  
  8497.     if (!traditional)
  8498.       hp->type = T_DISABLED;
  8499.   }
  8500. }
  8501.  
  8502. /*
  8503.  * Parse a macro argument and store the info on it into *ARGPTR.
  8504.  * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
  8505.  * Return nonzero to indicate a syntax error.
  8506.  */
  8507.  
  8508. static char *
  8509. macarg (argptr, rest_args)
  8510.      register struct argdata *argptr;
  8511.      int rest_args;
  8512. {
  8513.   FILE_BUF *ip = &instack[indepth];
  8514.   int paren = 0;
  8515.   int newlines = 0;
  8516.   int comments = 0;
  8517.   char *result = 0;
  8518.  
  8519.   /* Try to parse as much of the argument as exists at this
  8520.      input stack level.  */
  8521.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  8522.             &paren, &newlines, &comments, rest_args);
  8523.  
  8524.   /* If we find the end of the argument at this level,
  8525.      set up *ARGPTR to point at it in the input stack.  */
  8526.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  8527.       && bp != ip->buf + ip->length) {
  8528.     if (argptr != 0) {
  8529.       argptr->raw = ip->bufp;
  8530.       argptr->raw_length = bp - ip->bufp;
  8531.       argptr->newlines = newlines;
  8532.     }
  8533.     ip->bufp = bp;
  8534.   } else {
  8535.     /* This input stack level ends before the macro argument does.
  8536.        We must pop levels and keep parsing.
  8537.        Therefore, we must allocate a temporary buffer and copy
  8538.        the macro argument into it.  */
  8539.     int bufsize = bp - ip->bufp;
  8540.     int extra = newlines;
  8541.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  8542.     int final_start = 0;
  8543.  
  8544.     bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
  8545.     ip->bufp = bp;
  8546.     ip->lineno += newlines;
  8547.  
  8548.     while (bp == ip->buf + ip->length) {
  8549.       if (instack[indepth].macro == 0) {
  8550.     result = "unterminated macro call";
  8551.     break;
  8552.       }
  8553.       ip->macro->type = T_MACRO;
  8554.       if (ip->free_ptr)
  8555.     free (ip->free_ptr);
  8556.       ip = &instack[--indepth];
  8557.       newlines = 0;
  8558.       comments = 0;
  8559.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  8560.             &newlines, &comments, rest_args);
  8561.       final_start = bufsize;
  8562.       bufsize += bp - ip->bufp;
  8563.       extra += newlines;
  8564.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  8565.       bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
  8566.          bp - ip->bufp);
  8567.       ip->bufp = bp;
  8568.       ip->lineno += newlines;
  8569.     }
  8570.  
  8571.     /* Now, if arg is actually wanted, record its raw form,
  8572.        discarding comments and duplicating newlines in whatever
  8573.        part of it did not come from a macro expansion.
  8574.        EXTRA space has been preallocated for duplicating the newlines.
  8575.        FINAL_START is the index of the start of that part.  */
  8576.     if (argptr != 0) {
  8577.       argptr->raw = buffer;
  8578.       argptr->raw_length = bufsize;
  8579.       argptr->free1 = buffer;
  8580.       argptr->newlines = newlines;
  8581.       if ((newlines || comments) && ip->fname != 0)
  8582.     argptr->raw_length
  8583.       = final_start +
  8584.         discard_comments (argptr->raw + final_start,
  8585.                   argptr->raw_length - final_start,
  8586.                   newlines);
  8587.       argptr->raw[argptr->raw_length] = 0;
  8588.       if (argptr->raw_length > bufsize + extra)
  8589.     abort ();
  8590.     }
  8591.   }
  8592.  
  8593.   /* If we are not discarding this argument,
  8594.      macroexpand it and compute its length as stringified.
  8595.      All this info goes into *ARGPTR.  */
  8596.  
  8597.   if (argptr != 0) {
  8598.     register U_CHAR *buf, *lim;
  8599.     register int totlen;
  8600.  
  8601.     buf = argptr->raw;
  8602.     lim = buf + argptr->raw_length;
  8603.  
  8604.     while (buf != lim && is_space[*buf])
  8605.       buf++;
  8606.     while (buf != lim && is_space[lim[-1]])
  8607.       lim--;
  8608.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  8609.     while (buf != lim) {
  8610.       register U_CHAR c = *buf++;
  8611.       totlen++;
  8612.       /* Internal sequences of whitespace are replaced by one space
  8613.      in most cases, but not always.  So count all the whitespace
  8614.      in case we need to keep it all.  */
  8615. #if 0
  8616.       if (is_space[c])
  8617.     SKIP_ALL_WHITE_SPACE (buf);
  8618.       else
  8619. #endif
  8620.       if (c == '\"' || c == '\\') /* escape these chars */
  8621.     totlen++;
  8622.       else if (!isprint (c))
  8623.     totlen += 3;
  8624.     }
  8625.     argptr->stringified_length = totlen;
  8626.   }
  8627.   return result;
  8628. }
  8629.  
  8630. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  8631.    counting parens in *DEPTHPTR,
  8632.    and return if reach LIMIT
  8633.    or before a `)' that would make *DEPTHPTR negative
  8634.    or before a comma when *DEPTHPTR is zero.
  8635.    Single and double quotes are matched and termination
  8636.    is inhibited within them.  Comments also inhibit it.
  8637.    Value returned is pointer to stopping place.
  8638.  
  8639.    Increment *NEWLINES each time a newline is passed.
  8640.    REST_ARGS notifies macarg1 that it should absorb the rest of the args.
  8641.    Set *COMMENTS to 1 if a comment is seen.  */
  8642.  
  8643. static U_CHAR *
  8644. macarg1 (start, limit, depthptr, newlines, comments, rest_args)
  8645.      U_CHAR *start;
  8646.      register U_CHAR *limit;
  8647.      int *depthptr, *newlines, *comments;
  8648.      int rest_args;
  8649. {
  8650.   register U_CHAR *bp = start;
  8651.  
  8652.   while (bp < limit) {
  8653.     switch (*bp) {
  8654.     case '(':
  8655.       (*depthptr)++;
  8656.       break;
  8657.     case ')':
  8658.       if (--(*depthptr) < 0)
  8659.     return bp;
  8660.       break;
  8661.     case '\\':
  8662.       /* Traditionally, backslash makes following char not special.  */
  8663.       if (bp + 1 < limit && traditional)
  8664.     {
  8665.       bp++;
  8666.       /* But count source lines anyway.  */
  8667.       if (*bp == '\n')
  8668.         ++*newlines;
  8669.     }
  8670.       break;
  8671.     case '\n':
  8672.       ++*newlines;
  8673.       break;
  8674.     case '/':
  8675.       if (bp[1] == '\\' && bp[2] == '\n')
  8676.     newline_fix (bp + 1);
  8677.       if (cplusplus_comments && bp[1] == '/') {
  8678.     *comments = 1;
  8679.     bp += 2;
  8680.     while (bp < limit && (*bp != '\n' || bp[-1] == '\\')) {
  8681.       if (*bp == '\n') ++*newlines;
  8682.       bp++;
  8683.     }
  8684.     /* Now count the newline that we are about to skip.  */
  8685.     ++*newlines;
  8686.     break;
  8687.       }
  8688.       if (bp[1] != '*' || bp + 1 >= limit)
  8689.     break;
  8690.       *comments = 1;
  8691.       bp += 2;
  8692.       while (bp + 1 < limit) {
  8693.     if (bp[0] == '*'
  8694.         && bp[1] == '\\' && bp[2] == '\n')
  8695.       newline_fix (bp + 1);
  8696.     if (bp[0] == '*' && bp[1] == '/')
  8697.       break;
  8698.     if (*bp == '\n') ++*newlines;
  8699.     bp++;
  8700.       }
  8701.       break;
  8702.     case '\'':
  8703.     case '\"':
  8704.       {
  8705.     int quotec;
  8706.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  8707.       if (*bp == '\\') {
  8708.         bp++;
  8709.         if (*bp == '\n')
  8710.           ++*newlines;
  8711.         while (*bp == '\\' && bp[1] == '\n') {
  8712.           bp += 2;
  8713.         }
  8714.       } else if (*bp == '\n') {
  8715.         ++*newlines;
  8716.         if (quotec == '\'')
  8717.           break;
  8718.       }
  8719.     }
  8720.       }
  8721.       break;
  8722.     case ',':
  8723.       /* if we've returned to lowest level and we aren't absorbing all args */
  8724.       if ((*depthptr) == 0 && rest_args == 0)
  8725.     return bp;
  8726.       break;
  8727.     }
  8728.     bp++;
  8729.   }
  8730.  
  8731.   return bp;
  8732. }
  8733.  
  8734. /* Discard comments and duplicate newlines
  8735.    in the string of length LENGTH at START,
  8736.    except inside of string constants.
  8737.    The string is copied into itself with its beginning staying fixed.  
  8738.  
  8739.    NEWLINES is the number of newlines that must be duplicated.
  8740.    We assume that that much extra space is available past the end
  8741.    of the string.  */
  8742.  
  8743. static int
  8744. discard_comments (start, length, newlines)
  8745.      U_CHAR *start;
  8746.      int length;
  8747.      int newlines;
  8748. {
  8749.   register U_CHAR *ibp;
  8750.   register U_CHAR *obp;
  8751.   register U_CHAR *limit;
  8752.   register int c;
  8753.  
  8754.   /* If we have newlines to duplicate, copy everything
  8755.      that many characters up.  Then, in the second part,
  8756.      we will have room to insert the newlines
  8757.      while copying down.
  8758.      NEWLINES may actually be too large, because it counts
  8759.      newlines in string constants, and we don't duplicate those.
  8760.      But that does no harm.  */
  8761.   if (newlines > 0) {
  8762.     ibp = start + length;
  8763.     obp = ibp + newlines;
  8764.     limit = start;
  8765.     while (limit != ibp)
  8766.       *--obp = *--ibp;
  8767.   }
  8768.  
  8769.   ibp = start + newlines;
  8770.   limit = start + length + newlines;
  8771.   obp = start;
  8772.  
  8773.   while (ibp < limit) {
  8774.     *obp++ = c = *ibp++;
  8775.     switch (c) {
  8776.     case '\n':
  8777.       /* Duplicate the newline.  */
  8778.       *obp++ = '\n';
  8779.       break;
  8780.  
  8781.     case '\\':
  8782.       if (*ibp == '\n') {
  8783.     obp--;
  8784.     ibp++;
  8785.       }
  8786.       break;
  8787.  
  8788.     case '/':
  8789.       if (*ibp == '\\' && ibp[1] == '\n')
  8790.     newline_fix (ibp);
  8791.       /* Delete any comment.  */
  8792.       if (cplusplus_comments && ibp[0] == '/') {
  8793.     /* Comments are equivalent to spaces.  */
  8794.     obp[-1] = ' ';
  8795.     ibp++;
  8796.     while (ibp < limit && (*ibp != '\n' || ibp[-1] == '\\'))
  8797.       ibp++;
  8798.     break;
  8799.       }
  8800.       if (ibp[0] != '*' || ibp + 1 >= limit)
  8801.     break;
  8802.       /* Comments are equivalent to spaces.
  8803.      For -traditional, a comment is equivalent to nothing.  */
  8804.       if (traditional)
  8805.     obp--;
  8806.       else
  8807.     obp[-1] = ' ';
  8808.       ibp++;
  8809.       while (ibp + 1 < limit) {
  8810.     if (ibp[0] == '*'
  8811.         && ibp[1] == '\\' && ibp[2] == '\n')
  8812.       newline_fix (ibp + 1);
  8813.     if (ibp[0] == '*' && ibp[1] == '/')
  8814.       break;
  8815.     ibp++;
  8816.       }
  8817.       ibp += 2;
  8818.       break;
  8819.  
  8820.     case '\'':
  8821.     case '\"':
  8822.       /* Notice and skip strings, so that we don't
  8823.      think that comments start inside them,
  8824.      and so we don't duplicate newlines in them.  */
  8825.       {
  8826.     int quotec = c;
  8827.     while (ibp < limit) {
  8828.       *obp++ = c = *ibp++;
  8829.       if (c == quotec)
  8830.         break;
  8831.       if (c == '\n' && quotec == '\'')
  8832.         break;
  8833.       if (c == '\\' && ibp < limit) {
  8834.         while (*ibp == '\\' && ibp[1] == '\n')
  8835.           ibp += 2;
  8836.         *obp++ = *ibp++;
  8837.       }
  8838.     }
  8839.       }
  8840.       break;
  8841.     }
  8842.   }
  8843.  
  8844.   return obp - start;
  8845. }
  8846.  
  8847. /* Turn newlines to spaces in the string of length LENGTH at START,
  8848.    except inside of string constants.
  8849.    The string is copied into itself with its beginning staying fixed.  */
  8850.  
  8851. static int
  8852. change_newlines (start, length)
  8853.      U_CHAR *start;
  8854.      int length;
  8855. {
  8856.   register U_CHAR *ibp;
  8857.   register U_CHAR *obp;
  8858.   register U_CHAR *limit;
  8859.   register int c;
  8860.  
  8861.   ibp = start;
  8862.   limit = start + length;
  8863.   obp = start;
  8864.  
  8865.   while (ibp < limit) {
  8866.     *obp++ = c = *ibp++;
  8867.     switch (c) {
  8868.     case '\n':
  8869.       /* If this is a NEWLINE NEWLINE, then this is a real newline in the
  8870.      string.  Skip past the newline and its duplicate.
  8871.      Put a space in the output.  */
  8872.       if (*ibp == '\n')
  8873.     {
  8874.       ibp++;
  8875.       obp--;
  8876.       *obp++ = ' ';
  8877.     }
  8878.       break;
  8879.  
  8880.     case '\'':
  8881.     case '\"':
  8882.       /* Notice and skip strings, so that we don't delete newlines in them.  */
  8883.       {
  8884.     int quotec = c;
  8885.     while (ibp < limit) {
  8886.       *obp++ = c = *ibp++;
  8887.       if (c == quotec)
  8888.         break;
  8889.       if (c == '\n' && quotec == '\'')
  8890.         break;
  8891.     }
  8892.       }
  8893.       break;
  8894.     }
  8895.   }
  8896.  
  8897.   return obp - start;
  8898. }
  8899.  
  8900. /*
  8901.  * my_strerror - return the descriptive text associated with an `errno' code.
  8902.  */
  8903.  
  8904. char *
  8905. my_strerror (errnum)
  8906.      int errnum;
  8907. {
  8908.   char *result;
  8909.  
  8910. #ifndef VMS
  8911. #ifndef HAVE_STRERROR
  8912.   result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
  8913. #else
  8914.   result = strerror (errnum);
  8915. #endif
  8916. #else    /* VMS */
  8917.   /* VAXCRTL's strerror() takes an optional second argument, which only
  8918.      matters when the first argument is EVMSERR.  However, it's simplest
  8919.      just to pass it unconditionally.  `vaxc$errno' is declared in
  8920.      <errno.h>, and maintained by the library in parallel with `errno'.
  8921.      We assume that caller's `errnum' either matches the last setting of
  8922.      `errno' by the library or else does not have the value `EVMSERR'.  */
  8923.  
  8924.   result = strerror (errnum, vaxc$errno);
  8925. #endif
  8926.  
  8927.   if (!result)
  8928.     result = "undocumented I/O error";
  8929.  
  8930.   return result;
  8931. }
  8932.  
  8933. /*
  8934.  * error - print error message and increment count of errors.
  8935.  */
  8936.  
  8937. void
  8938. error (PRINTF_ALIST (msg))
  8939.      PRINTF_DCL (msg)
  8940. {
  8941.   va_list args;
  8942.  
  8943.   VA_START (args, msg);
  8944.   verror (msg, args);
  8945.   va_end (args);
  8946. }
  8947.  
  8948. static void
  8949. verror (msg, args)
  8950.      char *msg;
  8951.      va_list args;
  8952. {
  8953.   int i;
  8954.   FILE_BUF *ip = NULL;
  8955.  
  8956.   print_containing_files ();
  8957.  
  8958.   for (i = indepth; i >= 0; i--)
  8959.     if (instack[i].fname != NULL) {
  8960.       ip = &instack[i];
  8961.       break;
  8962.     }
  8963.  
  8964.   if (ip != NULL)
  8965.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8966.   vfprintf (stderr, msg, args);
  8967.   fprintf (stderr, "\n");
  8968.   errors++;
  8969. }
  8970.  
  8971. /* Error including a message from `errno'.  */
  8972.  
  8973. static void
  8974. error_from_errno (name)
  8975.      char *name;
  8976. {
  8977.   int i;
  8978.   FILE_BUF *ip = NULL;
  8979.  
  8980.   print_containing_files ();
  8981.  
  8982.   for (i = indepth; i >= 0; i--)
  8983.     if (instack[i].fname != NULL) {
  8984.       ip = &instack[i];
  8985.       break;
  8986.     }
  8987.  
  8988.   if (ip != NULL)
  8989.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8990.  
  8991.   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
  8992.  
  8993.   errors++;
  8994. }
  8995.  
  8996. /* Print error message but don't count it.  */
  8997.  
  8998. void
  8999. warning (PRINTF_ALIST (msg))
  9000.      PRINTF_DCL (msg)
  9001. {
  9002.   va_list args;
  9003.  
  9004.   VA_START (args, msg);
  9005.   vwarning (msg, args);
  9006.   va_end (args);
  9007. }
  9008.  
  9009. static void
  9010. vwarning (msg, args)
  9011.      char *msg;
  9012.      va_list args;
  9013. {
  9014.   int i;
  9015.   FILE_BUF *ip = NULL;
  9016.  
  9017.   if (inhibit_warnings)
  9018.     return;
  9019.  
  9020.   if (warnings_are_errors)
  9021.     errors++;
  9022.  
  9023.   print_containing_files ();
  9024.  
  9025.   for (i = indepth; i >= 0; i--)
  9026.     if (instack[i].fname != NULL) {
  9027.       ip = &instack[i];
  9028.       break;
  9029.     }
  9030.  
  9031.   if (ip != NULL)
  9032.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  9033.   fprintf (stderr, "warning: ");
  9034.   vfprintf (stderr, msg, args);
  9035.   fprintf (stderr, "\n");
  9036. }
  9037.  
  9038. static void
  9039. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9040. error_with_line (int line, PRINTF_ALIST (msg))
  9041. #else
  9042. error_with_line (line, PRINTF_ALIST (msg))
  9043.      int line;
  9044.      PRINTF_DCL (msg)
  9045. #endif
  9046. {
  9047.   va_list args;
  9048.  
  9049.   VA_START (args, msg);
  9050.   verror_with_line (line, msg, args);
  9051.   va_end (args);
  9052. }
  9053.  
  9054. static void
  9055. verror_with_line (line, msg, args)
  9056.      int line;
  9057.      char *msg;
  9058.      va_list args;
  9059. {
  9060.   int i;
  9061.   FILE_BUF *ip = NULL;
  9062.  
  9063.   print_containing_files ();
  9064.  
  9065.   for (i = indepth; i >= 0; i--)
  9066.     if (instack[i].fname != NULL) {
  9067.       ip = &instack[i];
  9068.       break;
  9069.     }
  9070.  
  9071.   if (ip != NULL)
  9072.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  9073.   vfprintf (stderr, msg, args);
  9074.   fprintf (stderr, "\n");
  9075.   errors++;
  9076. }
  9077.  
  9078. static void
  9079. vwarning_with_line (line, msg, args)
  9080.      int line;
  9081.      char *msg;
  9082.      va_list args;
  9083. {
  9084.   int i;
  9085.   FILE_BUF *ip = NULL;
  9086.  
  9087.   if (inhibit_warnings)
  9088.     return;
  9089.  
  9090.   if (warnings_are_errors)
  9091.     errors++;
  9092.  
  9093.   print_containing_files ();
  9094.  
  9095.   for (i = indepth; i >= 0; i--)
  9096.     if (instack[i].fname != NULL) {
  9097.       ip = &instack[i];
  9098.       break;
  9099.     }
  9100.  
  9101.   if (ip != NULL)
  9102.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  9103.   fprintf (stderr, "warning: ");
  9104.   vfprintf (stderr, msg, args);
  9105.   fprintf (stderr, "\n");
  9106. }
  9107.  
  9108. /* print an error message and maybe count it.  */
  9109.  
  9110. void
  9111. pedwarn (PRINTF_ALIST (msg))
  9112.      PRINTF_DCL (msg)
  9113. {
  9114.   va_list args;
  9115.  
  9116.   VA_START (args, msg);
  9117.   if (pedantic_errors)
  9118.     verror (msg, args);
  9119.   else
  9120.     vwarning (msg, args);
  9121.   va_end (args);
  9122. }
  9123.  
  9124. void
  9125. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9126. pedwarn_with_line (int line, PRINTF_ALIST (msg))
  9127. #else
  9128. pedwarn_with_line (line, PRINTF_ALIST (msg))
  9129.      int line;
  9130.      PRINTF_DCL (msg)
  9131. #endif
  9132. {
  9133.   va_list args;
  9134.  
  9135.   VA_START (args, msg);
  9136.   if (pedantic_errors)
  9137.     verror_with_line (line, msg, args);
  9138.   else
  9139.     vwarning_with_line (line, msg, args);
  9140.   va_end (args);
  9141. }
  9142.  
  9143. /* Report a warning (or an error if pedantic_errors)
  9144.    giving specified file name and line number, not current.  */
  9145.  
  9146. static void
  9147. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9148. pedwarn_with_file_and_line (char *file, int line, PRINTF_ALIST (msg))
  9149. #else
  9150. pedwarn_with_file_and_line (file, line, PRINTF_ALIST (msg))
  9151.      char *file;
  9152.      int line;
  9153.      PRINTF_DCL (msg)
  9154. #endif
  9155. {
  9156.   va_list args;
  9157.  
  9158.   if (!pedantic_errors && inhibit_warnings)
  9159.     return;
  9160.   if (file != NULL)
  9161.     fprintf (stderr, "%s:%d: ", file, line);
  9162.   if (pedantic_errors)
  9163.     errors++;
  9164.   if (!pedantic_errors)
  9165.     fprintf (stderr, "warning: ");
  9166.   VA_START (args, msg);
  9167.   vfprintf (stderr, msg, args);
  9168.   va_end (args);
  9169.   fprintf (stderr, "\n");
  9170. }
  9171.  
  9172. /* Print the file names and line numbers of the #include
  9173.    directives which led to the current file.  */
  9174.  
  9175. static void
  9176. print_containing_files ()
  9177. {
  9178.   FILE_BUF *ip = NULL;
  9179.   int i;
  9180.   int first = 1;
  9181.  
  9182.   /* If stack of files hasn't changed since we last printed
  9183.      this info, don't repeat it.  */
  9184.   if (last_error_tick == input_file_stack_tick)
  9185.     return;
  9186.  
  9187.   for (i = indepth; i >= 0; i--)
  9188.     if (instack[i].fname != NULL) {
  9189.       ip = &instack[i];
  9190.       break;
  9191.     }
  9192.  
  9193.   /* Give up if we don't find a source file.  */
  9194.   if (ip == NULL)
  9195.     return;
  9196.  
  9197.   /* Find the other, outer source files.  */
  9198.   for (i--; i >= 0; i--)
  9199.     if (instack[i].fname != NULL) {
  9200.       ip = &instack[i];
  9201.       if (first) {
  9202.     first = 0;
  9203.     fprintf (stderr, "In file included");
  9204.       } else {
  9205.     fprintf (stderr, ",\n                ");
  9206.       }
  9207.  
  9208.       fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
  9209.     }
  9210.   if (! first)
  9211.     fprintf (stderr, ":\n");
  9212.  
  9213.   /* Record we have printed the status as of this time.  */
  9214.   last_error_tick = input_file_stack_tick;
  9215. }
  9216.  
  9217. /* Return the line at which an error occurred.
  9218.    The error is not necessarily associated with the current spot
  9219.    in the input stack, so LINE says where.  LINE will have been
  9220.    copied from ip->lineno for the current input level.
  9221.    If the current level is for a file, we return LINE.
  9222.    But if the current level is not for a file, LINE is meaningless.
  9223.    In that case, we return the lineno of the innermost file.  */
  9224.  
  9225. static int
  9226. line_for_error (line)
  9227.      int line;
  9228. {
  9229.   int i;
  9230.   int line1 = line;
  9231.  
  9232.   for (i = indepth; i >= 0; ) {
  9233.     if (instack[i].fname != 0)
  9234.       return line1;
  9235.     i--;
  9236.     if (i < 0)
  9237.       return 0;
  9238.     line1 = instack[i].lineno;
  9239.   }
  9240.   abort ();
  9241.   /*NOTREACHED*/
  9242.   return 0;
  9243. }
  9244.  
  9245. /*
  9246.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  9247.  *
  9248.  * As things stand, nothing is ever placed in the output buffer to be
  9249.  * removed again except when it's KNOWN to be part of an identifier,
  9250.  * so flushing and moving down everything left, instead of expanding,
  9251.  * should work ok.
  9252.  */
  9253.  
  9254. /* You might think void was cleaner for the return type,
  9255.    but that would get type mismatch in check_expand in strict ANSI.  */
  9256. static int
  9257. grow_outbuf (obuf, needed)
  9258.      register FILE_BUF *obuf;
  9259.      register int needed;
  9260. {
  9261.   register U_CHAR *p;
  9262.   int minsize;
  9263.  
  9264.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  9265.     return 0;
  9266.  
  9267.   /* Make it at least twice as big as it is now.  */
  9268.   obuf->length *= 2;
  9269.   /* Make it have at least 150% of the free space we will need.  */
  9270.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  9271.   if (minsize > obuf->length)
  9272.     obuf->length = minsize;
  9273.  
  9274.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  9275.     memory_full ();
  9276.  
  9277.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  9278.   obuf->buf = p;
  9279.  
  9280.   return 0;
  9281. }
  9282.  
  9283. /* Symbol table for macro names and special symbols */
  9284.  
  9285. /*
  9286.  * install a name in the main hash table, even if it is already there.
  9287.  *   name stops with first non alphanumeric, except leading '#'.
  9288.  * caller must check against redefinition if that is desired.
  9289.  * delete_macro () removes things installed by install () in fifo order.
  9290.  * this is important because of the `defined' special symbol used
  9291.  * in #if, and also if pushdef/popdef directives are ever implemented.
  9292.  *
  9293.  * If LEN is >= 0, it is the length of the name.
  9294.  * Otherwise, compute the length by scanning the entire name.
  9295.  *
  9296.  * If HASH is >= 0, it is the precomputed hash code.
  9297.  * Otherwise, compute the hash code.
  9298.  */
  9299. static HASHNODE *
  9300. install (name, len, type, value, hash)
  9301.      U_CHAR *name;
  9302.      int len;
  9303.      enum node_type type;
  9304.      char *value;
  9305.      int hash;
  9306. {
  9307.   register HASHNODE *hp;
  9308.   register int i, bucket;
  9309.   register U_CHAR *p, *q;
  9310.  
  9311.   if (len < 0) {
  9312.     p = name;
  9313.     while (is_idchar[*p])
  9314.       p++;
  9315.     len = p - name;
  9316.   }
  9317.  
  9318.   if (hash < 0)
  9319.     hash = hashf (name, len, HASHSIZE);
  9320.  
  9321.   i = sizeof (HASHNODE) + len + 1;
  9322.   hp = (HASHNODE *) xmalloc (i);
  9323.   bucket = hash;
  9324.   hp->bucket_hdr = &hashtab[bucket];
  9325.   hp->next = hashtab[bucket];
  9326.   hashtab[bucket] = hp;
  9327.   hp->prev = NULL;
  9328.   if (hp->next != NULL)
  9329.     hp->next->prev = hp;
  9330.   hp->type = type;
  9331.   hp->length = len;
  9332.   hp->value.cpval = value;
  9333.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  9334.   p = hp->name;
  9335.   q = name;
  9336.   for (i = 0; i < len; i++)
  9337.     *p++ = *q++;
  9338.   hp->name[len] = 0;
  9339.   return hp;
  9340. }
  9341.  
  9342. /*
  9343.  * find the most recent hash node for name name (ending with first
  9344.  * non-identifier char) installed by install
  9345.  *
  9346.  * If LEN is >= 0, it is the length of the name.
  9347.  * Otherwise, compute the length by scanning the entire name.
  9348.  *
  9349.  * If HASH is >= 0, it is the precomputed hash code.
  9350.  * Otherwise, compute the hash code.
  9351.  */
  9352. HASHNODE *
  9353. lookup (name, len, hash)
  9354.      U_CHAR *name;
  9355.      int len;
  9356.      int hash;
  9357. {
  9358.   register U_CHAR *bp;
  9359.   register HASHNODE *bucket;
  9360.  
  9361.   if (len < 0) {
  9362.     for (bp = name; is_idchar[*bp]; bp++) ;
  9363.     len = bp - name;
  9364.   }
  9365.  
  9366.   if (hash < 0)
  9367.     hash = hashf (name, len, HASHSIZE);
  9368.  
  9369.   bucket = hashtab[hash];
  9370.   while (bucket) {
  9371.     if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
  9372.       return bucket;
  9373.     bucket = bucket->next;
  9374.   }
  9375.   return NULL;
  9376. }
  9377.  
  9378. /*
  9379.  * Delete a hash node.  Some weirdness to free junk from macros.
  9380.  * More such weirdness will have to be added if you define more hash
  9381.  * types that need it.
  9382.  */
  9383.  
  9384. /* Note that the DEFINITION of a macro is removed from the hash table
  9385.    but its storage is not freed.  This would be a storage leak
  9386.    except that it is not reasonable to keep undefining and redefining
  9387.    large numbers of macros many times.
  9388.    In any case, this is necessary, because a macro can be #undef'd
  9389.    in the middle of reading the arguments to a call to it.
  9390.    If #undef freed the DEFINITION, that would crash.  */
  9391.  
  9392. static void
  9393. delete_macro (hp)
  9394.      HASHNODE *hp;
  9395. {
  9396.  
  9397.   if (hp->prev != NULL)
  9398.     hp->prev->next = hp->next;
  9399.   if (hp->next != NULL)
  9400.     hp->next->prev = hp->prev;
  9401.  
  9402.   /* make sure that the bucket chain header that
  9403.      the deleted guy was on points to the right thing afterwards. */
  9404.   if (hp == *hp->bucket_hdr)
  9405.     *hp->bucket_hdr = hp->next;
  9406.  
  9407. #if 0
  9408.   if (hp->type == T_MACRO) {
  9409.     DEFINITION *d = hp->value.defn;
  9410.     struct reflist *ap, *nextap;
  9411.  
  9412.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  9413.       nextap = ap->next;
  9414.       free (ap);
  9415.     }
  9416.     free (d);
  9417.   }
  9418. #endif
  9419.   free (hp);
  9420. }
  9421.  
  9422. /*
  9423.  * return hash function on name.  must be compatible with the one
  9424.  * computed a step at a time, elsewhere
  9425.  */
  9426. static int
  9427. hashf (name, len, hashsize)
  9428.      register U_CHAR *name;
  9429.      register int len;
  9430.      int hashsize;
  9431. {
  9432.   register int r = 0;
  9433.  
  9434.   while (len--)
  9435.     r = HASHSTEP (r, *name++);
  9436.  
  9437.   return MAKE_POS (r) % hashsize;
  9438. }
  9439.  
  9440.  
  9441. /* Dump the definition of a single macro HP to OF.  */
  9442. static void
  9443. dump_single_macro (hp, of)
  9444.      register HASHNODE *hp;
  9445.      FILE *of;
  9446. {
  9447.   register DEFINITION *defn = hp->value.defn;
  9448.   struct reflist *ap;
  9449.   int offset;
  9450.   int concat;
  9451.  
  9452.  
  9453.   /* Print the definition of the macro HP.  */
  9454.  
  9455.   fprintf (of, "#define %s", hp->name);
  9456.  
  9457.   if (defn->nargs >= 0) {
  9458.     int i;
  9459.  
  9460.     fprintf (of, "(");
  9461.     for (i = 0; i < defn->nargs; i++) {
  9462.       dump_arg_n (defn, i, of);
  9463.       if (i + 1 < defn->nargs)
  9464.     fprintf (of, ", ");
  9465.     }
  9466.     fprintf (of, ")");
  9467.   }
  9468.  
  9469.   fprintf (of, " ");
  9470.  
  9471.   offset = 0;
  9472.   concat = 0;
  9473.   for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  9474.     dump_defn_1 (defn->expansion, offset, ap->nchars, of);
  9475.     offset += ap->nchars;
  9476.     if (!traditional) {
  9477.       if (ap->nchars != 0)
  9478.     concat = 0;
  9479.       if (ap->stringify) {
  9480.     switch (ap->stringify) {
  9481.      case SHARP_TOKEN: fprintf (of, "#"); break;
  9482.      case WHITE_SHARP_TOKEN: fprintf (of, "# "); break;
  9483.      case PERCENT_COLON_TOKEN: fprintf (of, "%%:"); break;
  9484.      case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%: "); break;
  9485.      default: abort ();
  9486.     }
  9487.       }
  9488.       if (ap->raw_before != 0) {
  9489.     if (concat) {
  9490.       switch (ap->raw_before) {
  9491.        case WHITE_SHARP_TOKEN:
  9492.        case WHITE_PERCENT_COLON_TOKEN:
  9493.         fprintf (of, " ");
  9494.         break;
  9495.        default:
  9496.         break;
  9497.       }
  9498.     } else {
  9499.       switch (ap->raw_before) {
  9500.        case SHARP_TOKEN: fprintf (of, "##"); break;
  9501.        case WHITE_SHARP_TOKEN: fprintf (of, "## "); break;
  9502.        case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
  9503.        case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%:%%: "); break;
  9504.        default: abort ();
  9505.       }
  9506.     }
  9507.       }
  9508.       concat = 0;
  9509.     }
  9510.     dump_arg_n (defn, ap->argno, of);
  9511.     if (!traditional && ap->raw_after != 0) {
  9512.       switch (ap->raw_after) {
  9513.        case SHARP_TOKEN: fprintf (of, "##"); break;
  9514.        case WHITE_SHARP_TOKEN: fprintf (of, " ##"); break;
  9515.        case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
  9516.        case WHITE_PERCENT_COLON_TOKEN: fprintf (of, " %%:%%:"); break;
  9517.        default: abort ();
  9518.       }
  9519.       concat = 1;
  9520.     }
  9521.   }
  9522.   dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
  9523.   fprintf (of, "\n");
  9524. }
  9525.  
  9526. /* Dump all macro definitions as #defines to stdout.  */
  9527.  
  9528. static void
  9529. dump_all_macros ()
  9530. {
  9531.   int bucket;
  9532.  
  9533.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  9534.     register HASHNODE *hp;
  9535.  
  9536.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  9537.       if (hp->type == T_MACRO)
  9538.     dump_single_macro (hp, stdout);
  9539.     }
  9540.   }
  9541. }
  9542.  
  9543. /* Output to OF a substring of a macro definition.
  9544.    BASE is the beginning of the definition.
  9545.    Output characters START thru LENGTH.
  9546.    Unless traditional, discard newlines outside of strings, thus
  9547.    converting funny-space markers to ordinary spaces.  */
  9548.  
  9549. static void
  9550. dump_defn_1 (base, start, length, of)
  9551.      U_CHAR *base;
  9552.      int start;
  9553.      int length;
  9554.      FILE *of;
  9555. {
  9556.   U_CHAR *p = base + start;
  9557.   U_CHAR *limit = base + start + length;
  9558.  
  9559.   if (traditional)
  9560.     fwrite (p, sizeof (*p), length, of);
  9561.   else {
  9562.     while (p < limit) {
  9563.       if (*p == '\"' || *p =='\'') {
  9564.     U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
  9565.                      NULL_PTR, NULL_PTR);
  9566.     fwrite (p, sizeof (*p), p1 - p, of);
  9567.     p = p1;
  9568.       } else {
  9569.     if (*p != '\n')
  9570.       putc (*p, of);
  9571.     p++;
  9572.       }
  9573.     }
  9574.   }
  9575. }
  9576.  
  9577. /* Print the name of argument number ARGNUM of macro definition DEFN
  9578.    to OF.
  9579.    Recall that DEFN->args.argnames contains all the arg names
  9580.    concatenated in reverse order with comma-space in between.  */
  9581.  
  9582. static void
  9583. dump_arg_n (defn, argnum, of)
  9584.      DEFINITION *defn;
  9585.      int argnum;
  9586.      FILE *of;
  9587. {
  9588.   register U_CHAR *p = defn->args.argnames;
  9589.   while (argnum + 1 < defn->nargs) {
  9590.     p = (U_CHAR *) index ((char *) p, ' ') + 1;
  9591.     argnum++;
  9592.   }
  9593.  
  9594.   while (*p && *p != ',') {
  9595.     putc (*p, of);
  9596.     p++;
  9597.   }
  9598. }
  9599.  
  9600. /* Initialize syntactic classifications of characters.  */
  9601.  
  9602. static void
  9603. initialize_char_syntax ()
  9604. {
  9605.   register int i;
  9606.  
  9607.   /*
  9608.    * Set up is_idchar and is_idstart tables.  These should be
  9609.    * faster than saying (is_alpha (c) || c == '_'), etc.
  9610.    * Set up these things before calling any routines tthat
  9611.    * refer to them.
  9612.    */
  9613.   for (i = 'a'; i <= 'z'; i++) {
  9614.     is_idchar[i - 'a' + 'A'] = 1;
  9615.     is_idchar[i] = 1;
  9616.     is_idstart[i - 'a' + 'A'] = 1;
  9617.     is_idstart[i] = 1;
  9618.   }
  9619.   for (i = '0'; i <= '9'; i++)
  9620.     is_idchar[i] = 1;
  9621.   is_idchar['_'] = 1;
  9622.   is_idstart['_'] = 1;
  9623.   is_idchar['$'] = dollars_in_ident;
  9624.   is_idstart['$'] = dollars_in_ident;
  9625.  
  9626.   /* horizontal space table */
  9627.   is_hor_space[' '] = 1;
  9628.   is_hor_space['\t'] = 1;
  9629.   is_hor_space['\v'] = 1;
  9630.   is_hor_space['\f'] = 1;
  9631.   is_hor_space['\r'] = 1;
  9632.  
  9633.   is_space[' '] = 1;
  9634.   is_space['\t'] = 1;
  9635.   is_space['\v'] = 1;
  9636.   is_space['\f'] = 1;
  9637.   is_space['\n'] = 1;
  9638.   is_space['\r'] = 1;
  9639.  
  9640.   char_name['\v'] = "vertical tab";
  9641.   char_name['\f'] = "formfeed";
  9642.   char_name['\r'] = "carriage return";
  9643. }
  9644.  
  9645. /* Initialize the built-in macros.  */
  9646.  
  9647. static void
  9648. initialize_builtins (inp, outp)
  9649.      FILE_BUF *inp;
  9650.      FILE_BUF *outp;
  9651. {
  9652.   install ((U_CHAR *) "__LINE__", -1, T_SPECLINE, NULL_PTR, -1);
  9653.   install ((U_CHAR *) "__DATE__", -1, T_DATE, NULL_PTR, -1);
  9654.   install ((U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1);
  9655.   install ((U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1);
  9656.   install ((U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1);
  9657.   install ((U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1);
  9658. #ifndef NO_BUILTIN_SIZE_TYPE
  9659.   install ((U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1);
  9660. #endif
  9661. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  9662.   install ((U_CHAR *) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, NULL_PTR, -1);
  9663. #endif
  9664.   install ((U_CHAR *) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE, NULL_PTR, -1);
  9665.   install ((U_CHAR *) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE,
  9666.        NULL_PTR, -1);
  9667.   install ((U_CHAR *) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE,
  9668.        NULL_PTR, -1);
  9669.   install ((U_CHAR *) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE,
  9670.        NULL_PTR, -1);
  9671.   install ((U_CHAR *) "__TIME__", -1, T_TIME, NULL_PTR, -1);
  9672.   if (!traditional) {
  9673.     install ((U_CHAR *) "__STDC__", -1, T_CONST, "1", -1);
  9674.     install ((U_CHAR *) "__STDC_VERSION__", -1, T_CONST, "199409L", -1);
  9675.   }
  9676.   if (objc)
  9677.     install ((U_CHAR *) "__OBJC__", -1, T_CONST, "1", -1);
  9678. /*  This is supplied using a -D by the compiler driver
  9679.     so that it is present only when truly compiling with GNU C.  */
  9680. /*  install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1);  */
  9681.  
  9682.   if (debug_output)
  9683.     {
  9684.       char directive[2048];
  9685.       U_CHAR *udirective = (U_CHAR *) directive;
  9686.       register struct directive *dp = &directive_table[0];
  9687.       struct tm *timebuf = timestamp ();
  9688.  
  9689.       sprintf (directive, " __BASE_FILE__ \"%s\"\n",
  9690.            instack[0].nominal_fname);
  9691.       output_line_directive (inp, outp, 0, same_file);
  9692.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9693.                outp, dp);
  9694.  
  9695.       sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
  9696.       output_line_directive (inp, outp, 0, same_file);
  9697.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9698.                outp, dp);
  9699.  
  9700. #ifndef NO_BUILTIN_SIZE_TYPE
  9701.       sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
  9702.       output_line_directive (inp, outp, 0, same_file);
  9703.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9704.                outp, dp);
  9705. #endif
  9706.  
  9707. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  9708.       sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
  9709.       output_line_directive (inp, outp, 0, same_file);
  9710.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9711.                outp, dp);
  9712. #endif
  9713.  
  9714.       sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
  9715.       output_line_directive (inp, outp, 0, same_file);
  9716.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9717.                outp, dp);
  9718.  
  9719.       sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
  9720.            monthnames[timebuf->tm_mon],
  9721.            timebuf->tm_mday, timebuf->tm_year + 1900);
  9722.       output_line_directive (inp, outp, 0, same_file);
  9723.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9724.                outp, dp);
  9725.  
  9726.       sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
  9727.            timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
  9728.       output_line_directive (inp, outp, 0, same_file);
  9729.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9730.                outp, dp);
  9731.  
  9732.       if (!traditional)
  9733.     {
  9734.           sprintf (directive, " __STDC__ 1");
  9735.           output_line_directive (inp, outp, 0, same_file);
  9736.           pass_thru_directive (udirective, &udirective[strlen (directive)],
  9737.                    outp, dp);
  9738.     }
  9739.       if (objc)
  9740.     {
  9741.           sprintf (directive, " __OBJC__ 1");
  9742.           output_line_directive (inp, outp, 0, same_file);
  9743.           pass_thru_directive (udirective, &udirective[strlen (directive)],
  9744.                    outp, dp);
  9745.     }
  9746.     }
  9747. }
  9748.  
  9749. /*
  9750.  * process a given definition string, for initialization
  9751.  * If STR is just an identifier, define it with value 1.
  9752.  * If STR has anything after the identifier, then it should
  9753.  * be identifier=definition.
  9754.  */
  9755.  
  9756. static void
  9757. make_definition (str, op)
  9758.      char *str;
  9759.      FILE_BUF *op;
  9760. {
  9761.   FILE_BUF *ip;
  9762.   struct directive *kt;
  9763.   U_CHAR *buf, *p;
  9764.  
  9765.   p = buf = (U_CHAR *) str;
  9766.   if (!is_idstart[*p]) {
  9767.     error ("malformed option `-D %s'", str);
  9768.     return;
  9769.   }
  9770.   while (is_idchar[*++p])
  9771.     ;
  9772.   if (*p == '(') {
  9773.     while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
  9774.       ;
  9775.     if (*p++ != ')')
  9776.       p = (U_CHAR *) str;            /* Error */
  9777.   }
  9778.   if (*p == 0) {
  9779.     buf = (U_CHAR *) alloca (p - buf + 4);
  9780.     strcpy ((char *)buf, str);
  9781.     strcat ((char *)buf, " 1");
  9782.   } else if (*p != '=') {
  9783.     error ("malformed option `-D %s'", str);
  9784.     return;
  9785.   } else {
  9786.     U_CHAR *q;
  9787.     /* Copy the entire option so we can modify it.  */
  9788.     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
  9789.     strncpy ((char *) buf, str, p - (U_CHAR *) str);
  9790.     /* Change the = to a space.  */
  9791.     buf[p - (U_CHAR *) str] = ' ';
  9792.     /* Scan for any backslash-newline and remove it.  */
  9793.     p++;
  9794.     q = &buf[p - (U_CHAR *) str];
  9795.     while (*p) {
  9796.       if (*p == '\"' || *p == '\'') {
  9797.     int unterminated = 0;
  9798.     U_CHAR *p1 = skip_quoted_string (p, p + strlen ((char *) p), 0,
  9799.                      NULL_PTR, NULL_PTR, &unterminated);
  9800.     if (unterminated)
  9801.       return;
  9802.     while (p != p1)
  9803.       if (*p == '\\' && p[1] == '\n')
  9804.         p += 2;
  9805.       else
  9806.         *q++ = *p++;
  9807.       } else if (*p == '\\' && p[1] == '\n')
  9808.     p += 2;
  9809.       /* Change newline chars into newline-markers.  */
  9810.       else if (*p == '\n')
  9811.     {
  9812.       *q++ = '\n';
  9813.       *q++ = '\n';
  9814.       p++;
  9815.     }
  9816.       else
  9817.     *q++ = *p++;
  9818.     }
  9819.     *q = 0;
  9820.   }
  9821.   
  9822.   ip = &instack[++indepth];
  9823.   ip->nominal_fname = ip->fname = "*Initialization*";
  9824.  
  9825.   ip->buf = ip->bufp = buf;
  9826.   ip->length = strlen ((char *) buf);
  9827.   ip->lineno = 1;
  9828.   ip->macro = 0;
  9829.   ip->free_ptr = 0;
  9830.   ip->if_stack = if_stack;
  9831.   ip->system_header_p = 0;
  9832.  
  9833.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  9834.     ;
  9835.  
  9836.   /* Pass NULL instead of OP, since this is a "predefined" macro.  */
  9837.   do_define (buf, buf + strlen ((char *) buf), NULL_PTR, kt);
  9838.   --indepth;
  9839. }
  9840.  
  9841. /* JF, this does the work for the -U option */
  9842.  
  9843. static void
  9844. make_undef (str, op)
  9845.      char *str;
  9846.      FILE_BUF *op;
  9847. {
  9848.   FILE_BUF *ip;
  9849.   struct directive *kt;
  9850.  
  9851.   ip = &instack[++indepth];
  9852.   ip->nominal_fname = ip->fname = "*undef*";
  9853.  
  9854.   ip->buf = ip->bufp = (U_CHAR *) str;
  9855.   ip->length = strlen (str);
  9856.   ip->lineno = 1;
  9857.   ip->macro = 0;
  9858.   ip->free_ptr = 0;
  9859.   ip->if_stack = if_stack;
  9860.   ip->system_header_p = 0;
  9861.  
  9862.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  9863.     ;
  9864.  
  9865.   do_undef ((U_CHAR *) str, (U_CHAR *) str + strlen (str), op, kt);
  9866.   --indepth;
  9867. }
  9868.  
  9869. /* Process the string STR as if it appeared as the body of a #assert.
  9870.    OPTION is the option name for which STR was the argument.  */
  9871.  
  9872. static void
  9873. make_assertion (option, str)
  9874.      char *option;
  9875.      char *str;
  9876. {
  9877.   FILE_BUF *ip;
  9878.   struct directive *kt;
  9879.   U_CHAR *buf, *p, *q;
  9880.  
  9881.   /* Copy the entire option so we can modify it.  */
  9882.   buf = (U_CHAR *) alloca (strlen (str) + 1);
  9883.   strcpy ((char *) buf, str);
  9884.   /* Scan for any backslash-newline and remove it.  */
  9885.   p = q = buf;
  9886.   while (*p) {
  9887.     if (*p == '\\' && p[1] == '\n')
  9888.       p += 2;
  9889.     else
  9890.       *q++ = *p++;
  9891.   }
  9892.   *q = 0;
  9893.  
  9894.   p = buf;
  9895.   if (!is_idstart[*p]) {
  9896.     error ("malformed option `%s %s'", option, str);
  9897.     return;
  9898.   }
  9899.   while (is_idchar[*++p])
  9900.     ;
  9901.   SKIP_WHITE_SPACE (p);
  9902.   if (! (*p == 0 || *p == '(')) {
  9903.     error ("malformed option `%s %s'", option, str);
  9904.     return;
  9905.   }
  9906.   
  9907.   ip = &instack[++indepth];
  9908.   ip->nominal_fname = ip->fname = "*Initialization*";
  9909.  
  9910.   ip->buf = ip->bufp = buf;
  9911.   ip->length = strlen ((char *) buf);
  9912.   ip->lineno = 1;
  9913.   ip->macro = 0;
  9914.   ip->free_ptr = 0;
  9915.   ip->if_stack = if_stack;
  9916.   ip->system_header_p = 0;
  9917.  
  9918.   for (kt = directive_table; kt->type != T_ASSERT; kt++)
  9919.     ;
  9920.  
  9921.   /* pass NULL as output ptr to do_define since we KNOW it never
  9922.      does any output.... */
  9923.   do_assert (buf, buf + strlen ((char *) buf) , NULL_PTR, kt);
  9924.   --indepth;
  9925. }
  9926.  
  9927. /* Append a chain of `struct file_name_list's
  9928.    to the end of the main include chain.
  9929.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  9930.  
  9931. static void
  9932. append_include_chain (first, last)
  9933.      struct file_name_list *first, *last;
  9934. {
  9935.   struct file_name_list *dir;
  9936.  
  9937.   if (!first || !last)
  9938.     return;
  9939.  
  9940.   if (include == 0)
  9941.     include = first;
  9942.   else
  9943.     last_include->next = first;
  9944.  
  9945.   if (first_bracket_include == 0)
  9946.     first_bracket_include = first;
  9947.  
  9948.   for (dir = first; ; dir = dir->next) {
  9949.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  9950.     if (len > max_include_len)
  9951.       max_include_len = len;
  9952.     if (dir == last)
  9953.       break;
  9954.   }
  9955.  
  9956.   last->next = NULL;
  9957.   last_include = last;
  9958. }
  9959.  
  9960. /* Add output to `deps_buffer' for the -M switch.
  9961.    STRING points to the text to be output.
  9962.    SPACER is ':' for targets, ' ' for dependencies.  */
  9963.  
  9964. static void
  9965. deps_output (string, spacer)
  9966.      char *string;
  9967.      int spacer;
  9968. {
  9969.   int size = strlen (string);
  9970.  
  9971.   if (size == 0)
  9972.     return;
  9973.  
  9974. #ifndef MAX_OUTPUT_COLUMNS
  9975. #define MAX_OUTPUT_COLUMNS 72
  9976. #endif
  9977.   if (MAX_OUTPUT_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < deps_column + size
  9978.       && 1 < deps_column) {
  9979.     bcopy (" \\\n ", &deps_buffer[deps_size], 4);
  9980.     deps_size += 4;
  9981.     deps_column = 1;
  9982.     if (spacer == ' ')
  9983.       spacer = 0;
  9984.   }
  9985.  
  9986.   if (deps_size + size + 8 > deps_allocated_size) {
  9987.     deps_allocated_size = (deps_size + size + 50) * 2;
  9988.     deps_buffer = xrealloc (deps_buffer, deps_allocated_size);
  9989.   }
  9990.   if (spacer == ' ') {
  9991.     deps_buffer[deps_size++] = ' ';
  9992.     deps_column++;
  9993.   }
  9994.   bcopy (string, &deps_buffer[deps_size], size);
  9995.   deps_size += size;
  9996.   deps_column += size;
  9997.   if (spacer == ':') {
  9998.     deps_buffer[deps_size++] = ':';
  9999.     deps_column++;
  10000.   }
  10001.   deps_buffer[deps_size] = 0;
  10002. }
  10003.  
  10004. static void
  10005. fatal (PRINTF_ALIST (msg))
  10006.      PRINTF_DCL (msg)
  10007. {
  10008.   va_list args;
  10009.  
  10010. /* PhB 25-Jun-95: restore old process priority before exiting */
  10011. #ifdef __amigados__
  10012.   SetTaskPri(amiga_task, amiga_old_priority);
  10013. #endif /* __amigados__ */
  10014.  
  10015.   fprintf (stderr, "%s: ", progname);
  10016.   VA_START (args, msg);
  10017.   vfprintf (stderr, msg, args);
  10018.   va_end (args);
  10019.   fprintf (stderr, "\n");
  10020.   exit (FATAL_EXIT_CODE);
  10021. }
  10022.  
  10023. /* More 'friendly' abort that prints the line and file.
  10024.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  10025.  
  10026. void
  10027. fancy_abort ()
  10028. {
  10029.   fatal ("Internal gcc abort.");
  10030. }
  10031.  
  10032. static void
  10033. perror_with_name (name)
  10034.      char *name;
  10035. {
  10036.   fprintf (stderr, "%s: ", progname);
  10037.   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
  10038.   errors++;
  10039. }
  10040.  
  10041. static void
  10042. pfatal_with_name (name)
  10043.      char *name;
  10044. {
  10045.   perror_with_name (name);
  10046. #ifdef VMS
  10047.   exit (vaxc$errno);
  10048. #else
  10049.   exit (FATAL_EXIT_CODE);
  10050. #endif
  10051. }
  10052.  
  10053. /* Handler for SIGPIPE.  */
  10054.  
  10055. static void
  10056. pipe_closed (signo)
  10057.      /* If this is missing, some compilers complain.  */
  10058.      int signo;
  10059. {
  10060.   fatal ("output pipe has been closed");
  10061. }
  10062.  
  10063. static void
  10064. memory_full ()
  10065. {
  10066.   fatal ("Memory exhausted.");
  10067. }
  10068.  
  10069.  
  10070. GENERIC_PTR
  10071. xmalloc (size)
  10072.      size_t size;
  10073. {
  10074.   register GENERIC_PTR ptr = (GENERIC_PTR) malloc (size);
  10075.   if (!ptr)
  10076.     memory_full ();
  10077.   return ptr;
  10078. }
  10079.  
  10080. static GENERIC_PTR
  10081. xrealloc (old, size)
  10082.      GENERIC_PTR old;
  10083.      size_t size;
  10084. {
  10085.   register GENERIC_PTR ptr = (GENERIC_PTR) realloc (old, size);
  10086.   if (!ptr)
  10087.     memory_full ();
  10088.   return ptr;
  10089. }
  10090.  
  10091. static GENERIC_PTR
  10092. xcalloc (number, size)
  10093.      size_t number, size;
  10094. {
  10095.   register size_t total = number * size;
  10096.   register GENERIC_PTR ptr = (GENERIC_PTR) malloc (total);
  10097.   if (!ptr)
  10098.     memory_full ();
  10099.   bzero (ptr, total);
  10100.   return ptr;
  10101. }
  10102.  
  10103. static char *
  10104. savestring (input)
  10105.      char *input;
  10106. {
  10107.   size_t size = strlen (input);
  10108.   char *output = xmalloc (size + 1);
  10109.   strcpy (output, input);
  10110.   return output;
  10111. }
  10112.  
  10113. /* Get the file-mode and data size of the file open on FD
  10114.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  10115.  
  10116. static int
  10117. file_size_and_mode (fd, mode_pointer, size_pointer)
  10118.      int fd;
  10119.      int *mode_pointer;
  10120.      long int *size_pointer;
  10121. {
  10122.   struct stat sbuf;
  10123.  
  10124.   if (fstat (fd, &sbuf) < 0) return (-1);
  10125.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  10126.   if (size_pointer) *size_pointer = sbuf.st_size;
  10127.   return 0;
  10128. }
  10129.  
  10130. static void
  10131. output_dots (fd, depth)
  10132.      FILE* fd;
  10133.      int depth;
  10134. {
  10135.   while (depth > 0) {
  10136.     putc ('.', fd);
  10137.     depth--;
  10138.   }
  10139. }
  10140.   
  10141.  
  10142. #ifdef VMS
  10143.  
  10144. /* Under VMS we need to fix up the "include" specification
  10145.    filename so that everything following the 1st slash is
  10146.    changed into its correct VMS file specification. */
  10147.  
  10148. static void
  10149. hack_vms_include_specification (fname)
  10150.      char *fname;
  10151. {
  10152.   register char *cp, *cp1, *cp2;
  10153.   int f, check_filename_before_returning, no_prefix_seen;
  10154.   char Local[512];
  10155.  
  10156.   check_filename_before_returning = 0;
  10157.   no_prefix_seen = 0;
  10158.  
  10159.   /* Ignore leading "./"s */
  10160.   while (fname[0] == '.' && fname[1] == '/') {
  10161.     strcpy (fname, fname+2);
  10162.     no_prefix_seen = 1;        /* mark this for later */
  10163.   }
  10164.   /* Look for the boundary between the VMS and UNIX filespecs */
  10165.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  10166.   if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto            */
  10167.   if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
  10168.   if (cp) {
  10169.     cp++;
  10170.   } else {
  10171.     cp = index (fname, '/');    /* Look for the "/" */
  10172.   }
  10173.  
  10174.   /*
  10175.    * Check if we have a vax-c style '#include filename'
  10176.    * and add the missing .h
  10177.    */
  10178.   if (cp == 0) {
  10179.     if (index(fname,'.') == 0)
  10180.       strcat(fname, ".h");
  10181.   } else {
  10182.     if (index(cp,'.') == 0)
  10183.       strcat(cp, ".h");
  10184.   }
  10185.  
  10186.   cp2 = Local;            /* initialize */
  10187.  
  10188.   /* We are trying to do a number of things here.  First of all, we are
  10189.      trying to hammer the filenames into a standard format, such that later
  10190.      processing can handle them.
  10191.      
  10192.      If the file name contains something like [dir.], then it recognizes this
  10193.      as a root, and strips the ".]".  Later processing will add whatever is
  10194.      needed to get things working properly.
  10195.      
  10196.      If no device is specified, then the first directory name is taken to be
  10197.      a device name (or a rooted logical). */
  10198.  
  10199.   /* See if we found that 1st slash */
  10200.   if (cp == 0) return;        /* Nothing to do!!! */
  10201.   if (*cp != '/') return;    /* Nothing to do!!! */
  10202.   /* Point to the UNIX filename part (which needs to be fixed!) */
  10203.   cp1 = cp+1;
  10204.   /* If the directory spec is not rooted, we can just copy
  10205.      the UNIX filename part and we are done */
  10206.   if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
  10207.     if (cp[-2] != '.') {
  10208.       /*
  10209.        * The VMS part ends in a `]', and the preceding character is not a `.'.
  10210.        * We strip the `]', and then splice the two parts of the name in the
  10211.        * usual way.  Given the default locations for include files in cccp.c,
  10212.        * we will only use this code if the user specifies alternate locations
  10213.        * with the /include (-I) switch on the command line.  */
  10214.       cp -= 1;            /* Strip "]" */
  10215.       cp1--;            /* backspace */
  10216.     } else {
  10217.       /*
  10218.        * The VMS part has a ".]" at the end, and this will not do.  Later
  10219.        * processing will add a second directory spec, and this would be a syntax
  10220.        * error.  Thus we strip the ".]", and thus merge the directory specs.
  10221.        * We also backspace cp1, so that it points to a '/'.  This inhibits the
  10222.        * generation of the 000000 root directory spec (which does not belong here
  10223.        * in this case).
  10224.        */
  10225.       cp -= 2;            /* Strip ".]" */
  10226.       cp1--; };            /* backspace */
  10227.   } else {
  10228.  
  10229.     /* We drop in here if there is no VMS style directory specification yet.
  10230.      * If there is no device specification either, we make the first dir a
  10231.      * device and try that.  If we do not do this, then we will be essentially
  10232.      * searching the users default directory (as if they did a #include "asdf.h").
  10233.      *
  10234.      * Then all we need to do is to push a '[' into the output string. Later
  10235.      * processing will fill this in, and close the bracket.
  10236.      */
  10237.     if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec.  take first dir */
  10238.     *cp2++ = '[';        /* Open the directory specification */
  10239.   }
  10240.  
  10241.   /* at this point we assume that we have the device spec, and (at least
  10242.      the opening "[" for a directory specification.  We may have directories
  10243.      specified already */
  10244.  
  10245.   /* If there are no other slashes then the filename will be
  10246.      in the "root" directory.  Otherwise, we need to add
  10247.      directory specifications. */
  10248.   if (index (cp1, '/') == 0) {
  10249.     /* Just add "000000]" as the directory string */
  10250.     strcpy (cp2, "000000]");
  10251.     cp2 += strlen (cp2);
  10252.     check_filename_before_returning = 1; /* we might need to fool with this later */
  10253.   } else {
  10254.     /* As long as there are still subdirectories to add, do them. */
  10255.     while (index (cp1, '/') != 0) {
  10256.       /* If this token is "." we can ignore it */
  10257.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  10258.     cp1 += 2;
  10259.     continue;
  10260.       }
  10261.       /* Add a subdirectory spec. Do not duplicate "." */
  10262.       if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
  10263.     *cp2++ = '.';
  10264.       /* If this is ".." then the spec becomes "-" */
  10265.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  10266.     /* Add "-" and skip the ".." */
  10267.     *cp2++ = '-';
  10268.     cp1 += 3;
  10269.     continue;
  10270.       }
  10271.       /* Copy the subdirectory */
  10272.       while (*cp1 != '/') *cp2++= *cp1++;
  10273.       cp1++;            /* Skip the "/" */
  10274.     }
  10275.     /* Close the directory specification */
  10276.     if (cp2[-1] == '.')        /* no trailing periods */
  10277.       cp2--;
  10278.     *cp2++ = ']';
  10279.   }
  10280.   /* Now add the filename */
  10281.   while (*cp1) *cp2++ = *cp1++;
  10282.   *cp2 = 0;
  10283.   /* Now append it to the original VMS spec. */
  10284.   strcpy (cp, Local);
  10285.  
  10286.   /* If we put a [000000] in the filename, try to open it first. If this fails,
  10287.      remove the [000000], and return that name.  This provides flexibility
  10288.      to the user in that they can use both rooted and non-rooted logical names
  10289.      to point to the location of the file.  */
  10290.  
  10291.   if (check_filename_before_returning && no_prefix_seen) {
  10292.     f = open (fname, O_RDONLY, 0666);
  10293.     if (f >= 0) {
  10294.       /* The file name is OK as it is, so return it as is.  */
  10295.       close (f);
  10296.       return;
  10297.     }
  10298.     /* The filename did not work.  Try to remove the [000000] from the name,
  10299.        and return it.  */
  10300.     cp = index (fname, '[');
  10301.     cp2 = index (fname, ']') + 1;
  10302.     strcpy (cp, cp2);        /* this gets rid of it */
  10303.   }
  10304.   return;
  10305. }
  10306. #endif    /* VMS */
  10307.  
  10308. #ifdef    VMS
  10309.  
  10310. /* These are the read/write replacement routines for
  10311.    VAX-11 "C".  They make read/write behave enough
  10312.    like their UNIX counterparts that CCCP will work */
  10313.  
  10314. static int
  10315. read (fd, buf, size)
  10316.      int fd;
  10317.      char *buf;
  10318.      int size;
  10319. {
  10320. #undef    read    /* Get back the REAL read routine */
  10321.   register int i;
  10322.   register int total = 0;
  10323.  
  10324.   /* Read until the buffer is exhausted */
  10325.   while (size > 0) {
  10326.     /* Limit each read to 32KB */
  10327.     i = (size > (32*1024)) ? (32*1024) : size;
  10328.     i = read (fd, buf, i);
  10329.     if (i <= 0) {
  10330.       if (i == 0) return (total);
  10331.       return (i);
  10332.     }
  10333.     /* Account for this read */
  10334.     total += i;
  10335.     buf += i;
  10336.     size -= i;
  10337.   }
  10338.   return (total);
  10339. }
  10340.  
  10341. static int
  10342. write (fd, buf, size)
  10343.      int fd;
  10344.      char *buf;
  10345.      int size;
  10346. {
  10347. #undef    write    /* Get back the REAL write routine */
  10348.   int i;
  10349.   int j;
  10350.  
  10351.   /* Limit individual writes to 32Kb */
  10352.   i = size;
  10353.   while (i > 0) {
  10354.     j = (i > (32*1024)) ? (32*1024) : i;
  10355.     if (write (fd, buf, j) < 0) return (-1);
  10356.     /* Account for the data written */
  10357.     buf += j;
  10358.     i -= j;
  10359.   }
  10360.   return (size);
  10361. }
  10362.  
  10363. /* The following wrapper functions supply additional arguments to the VMS
  10364.    I/O routines to optimize performance with file handling.  The arguments
  10365.    are:
  10366.      "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
  10367.      "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
  10368.      "fop=tef"- Truncate unused portions of file when closing file.
  10369.      "shr=nil"- Disallow file sharing while file is open.
  10370.  */
  10371.  
  10372. static FILE *
  10373. freopen (fname, type, oldfile)
  10374.      char *fname;
  10375.      char *type;
  10376.      FILE *oldfile;
  10377. {
  10378. #undef    freopen    /* Get back the REAL fopen routine */
  10379.   if (strcmp (type, "w") == 0)
  10380.     return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  10381.   return freopen (fname, type, oldfile, "mbc=16");
  10382. }
  10383.  
  10384. static FILE *
  10385. fopen (fname, type)
  10386.      char *fname;
  10387.      char *type;
  10388. {
  10389. #undef fopen    /* Get back the REAL fopen routine */
  10390.   /* The gcc-vms-1.42 distribution's header files prototype fopen with two
  10391.      fixed arguments, which matches ANSI's specification but not VAXCRTL's
  10392.      pre-ANSI implementation.  This hack circumvents the mismatch problem.  */
  10393.   FILE *(*vmslib_fopen)() = (FILE *(*)()) fopen;
  10394.  
  10395.   if (*type == 'w')
  10396.     return (*vmslib_fopen) (fname, type, "mbc=32",
  10397.                 "deq=64", "fop=tef", "shr=nil");
  10398.   else
  10399.     return (*vmslib_fopen) (fname, type, "mbc=32");
  10400. }
  10401.  
  10402. static int 
  10403. open (fname, flags, prot)
  10404.      char *fname;
  10405.      int flags;
  10406.      int prot;
  10407. {
  10408. #undef open    /* Get back the REAL open routine */
  10409.   return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
  10410. }
  10411.  
  10412. /* Avoid run-time library bug, where copying M out of N+M characters with
  10413.    N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
  10414.    gcc-cpp exercises this particular bug.  [Fixed in V5.5-2's VAXCRTL.]  */
  10415.  
  10416. static char *
  10417. strncat (dst, src, cnt)
  10418.      char *dst;
  10419.      const char *src;
  10420.      unsigned cnt;
  10421. {
  10422.   register char *d = dst, *s = (char *) src;
  10423.   register int n = cnt;    /* convert to _signed_ type */
  10424.  
  10425.   while (*d) d++;    /* advance to end */
  10426.   while (--n >= 0)
  10427.     if (!(*d++ = *s++)) break;
  10428.   if (n < 0) *d = '\0';
  10429.   return dst;
  10430. }
  10431.  
  10432. /* more VMS hackery */
  10433. #include <fab.h>
  10434. #include <nam.h>
  10435.  
  10436. extern unsigned long sys$parse(), sys$search();
  10437.  
  10438. /* Work around another library bug.  If a file is located via a searchlist,
  10439.    and if the device it's on is not the same device as the one specified
  10440.    in the first element of that searchlist, then both stat() and fstat()
  10441.    will fail to return info about it.  `errno' will be set to EVMSERR, and
  10442.    `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
  10443.    We can get around this by fully parsing the filename and then passing
  10444.    that absolute name to stat().
  10445.  
  10446.    Without this fix, we can end up failing to find header files, which is
  10447.    bad enough, but then compounding the problem by reporting the reason for
  10448.    failure as "normal successful completion."  */
  10449.  
  10450. #undef fstat    /* get back to library version */
  10451.  
  10452. static int
  10453. VMS_fstat (fd, statbuf)
  10454.      int fd;
  10455.      struct stat *statbuf;
  10456. {
  10457.   int result = fstat (fd, statbuf);
  10458.  
  10459.   if (result < 0)
  10460.     {
  10461.       FILE *fp;
  10462.       char nambuf[NAM$C_MAXRSS+1];
  10463.  
  10464.       if ((fp = fdopen (fd, "r")) != 0 && fgetname (fp, nambuf) != 0)
  10465.     result = VMS_stat (nambuf, statbuf);
  10466.       /* No fclose(fp) here; that would close(fd) as well.  */
  10467.     }
  10468.  
  10469.   return result;
  10470. }
  10471.  
  10472. static int
  10473. VMS_stat (name, statbuf)
  10474.      const char *name;
  10475.      struct stat *statbuf;
  10476. {
  10477.   int result = stat (name, statbuf);
  10478.  
  10479.   if (result < 0)
  10480.     {
  10481.       struct FAB fab;
  10482.       struct NAM nam;
  10483.       char exp_nam[NAM$C_MAXRSS+1],  /* expanded name buffer for sys$parse */
  10484.        res_nam[NAM$C_MAXRSS+1];  /* resultant name buffer for sys$search */
  10485.  
  10486.       fab = cc$rms_fab;
  10487.       fab.fab$l_fna = (char *) name;
  10488.       fab.fab$b_fns = (unsigned char) strlen (name);
  10489.       fab.fab$l_nam = (void *) &nam;
  10490.       nam = cc$rms_nam;
  10491.       nam.nam$l_esa = exp_nam,  nam.nam$b_ess = sizeof exp_nam - 1;
  10492.       nam.nam$l_rsa = res_nam,  nam.nam$b_rss = sizeof res_nam - 1;
  10493.       nam.nam$b_nop = NAM$M_PWD | NAM$M_NOCONCEAL;
  10494.       if (sys$parse (&fab) & 1)
  10495.     {
  10496.       if (sys$search (&fab) & 1)
  10497.         {
  10498.           res_nam[nam.nam$b_rsl] = '\0';
  10499.           result = stat (res_nam, statbuf);
  10500.         }
  10501.       /* Clean up searchlist context cached by the system.  */
  10502.       nam.nam$b_nop = NAM$M_SYNCHK;
  10503.       fab.fab$l_fna = 0,  fab.fab$b_fns = 0;
  10504.       (void) sys$parse (&fab);
  10505.     }
  10506.     }
  10507.  
  10508.   return result;
  10509. }
  10510. #endif /* VMS */
  10511.  
  10512.  
  10513. #ifdef __amigados__
  10514.  
  10515. /* This function returns whether the LEN characters long filename FNAME 
  10516.    is an absolute path specification. */
  10517.  
  10518. static int
  10519. amigados_abs_filename (fname, len)
  10520.      char *fname;
  10521.      int len;
  10522. {
  10523.   /* we're using ixemul.library, which treats `/foo' as `foo:', so 
  10524.      fname[0] is to be considered absolute as well */
  10525.   if (fname[0] == '/')
  10526.     return 1;
  10527.  
  10528.   /* else do an index() on fname, but one which is limited to len characters */
  10529.   while (*fname && *fname != ':' && len) 
  10530.     fname++, len--;
  10531.  
  10532.   return *fname == ':';
  10533. }
  10534. #endif /* __amigados__ */
  10535.